基于HCL的python脚本编程(4) H3CNERS00
兰生幽谷,不为莫服而不芳;
君子行义,不为莫知而止休。
向H3C金牌讲师邓方鸣老师致敬
工欲善其事,必先利其器
搭建的"网络基础设施"如下图所示:

图 2- 1网络基础设施
2.1 网络设备基础设施配置python脚本
在做网络实验前,需要给各个交换机配置管理功能。我默认192.168.100.0/24为管理网段,网关为宿主机~.1/24。除了给交换机和路由器配置管理ip外,剩下的工作,就是给交换机和路由器配置telnet和ssh。以后所有的服务,没有特殊说明,用户名都是设备名,口令都是Admin@12345.
| 设备名称 | 管理IP | 管理用户名 | 口令 | ||
|---|---|---|---|---|---|
| SW0 | 192.168.100.165 | SW0 | Admin@12345 | ||
| SW1 | 192.168.100.160 | SW1 | Admin@12345 | ||
| SW2 | 192.168.100.170 | SW2 | Admin@12345 | ||
| R1 | 192.168.100.161 | R1 | Admin@12345 | ||
| R2 | 192.168.100.162 | R2 | Admin@12345 | ||
| R3 | 192.168.100.163 | R3 | Admin@12345 | ||
| R4 | 192.168.100.164 | R4 | Admin@12345 | ||
| R5 | 192.168.100.166 | R5 | Admin@12345 | ||
| R11 | 192.168.100.171 | R11 | Admin@12345 | ||
| R12 | 192.168.100.172 | R12 | Admin@12345 | ||
| R13 | 192.168.100.173 | R13 | Admin@12345 | ||
| R14 | 192.168.100.174 | R14 | Admin@12345 | ||
| R15 | 192.168.100.175 | R15 | Admin@12345 |
1)路由器
进入系统视图,将下面的脚本改成适合自己的参数,粘贴即可:
python
#1.起名
sysname R1
#2.设ip
interface GigabitEthernet0/0
ip address 192.168.100.161 255.255.255.0
quit
#3.管理名、口令、服务类型、授权
local-user R1 class manage
password simple Admin@12345
service-type telnet ssh
authorization-attribute user-role network-admin
undo authorization-attribute user-role network-operator
quit
#4.虚拟终端
line vty 0 63
authentication-mode scheme
protocol inbound all
user-role network-admin
idle-timeout 45 0
quit
#5.启动服务
telnet server enable
ssh server enable
ssh server authentication-retries 3
public-key local create rsa
return
save force
2)交换机
和路由器一样,改动相应参数,进入系统视图后,粘贴:
python
sysn SW1
int vlan 1
ip address 192.168.100.160 255.255.255.0
quit
local-user SW1 class manage
password simple Admin@12345
service-type telnet ssh
authorization-attribute user-role network-admin
undo authorization-attribute user-role network-operator
quit
line vty 0 63
authentication-mode scheme
protocol inbound all
user-role network-admin
undo user-role network-operator
idle-timeout 45 0
quit
telnet server enable
ssh server enable
ssh server authentication-retries 3
public-key local create rsa
return
save force

图 2- 2

图 2- 3

图 2- 4

图 2- 5
2.2 Python脚本配置登录ftp网站
先看脚本:
python
# -*- coding: utf-8 -*-
# @FilePath : /h3hcl/hcl-lab/scripts/p0201ftp.py
# @Author : Wu@cqie
# @Date : 2026-10-29 10:20:33
# @LastEditTime : 2026-10-29 10:33:16
# @LastEditors : Wu@cqie
# @Version : 1.0.0
# @Description : 开启 FTP 服务,并创建 FTP 用户(复用本地用户 R1)
# @License : CQIE
#
#1.导入
# ConnectHandler ------ netmiko 的万能连接工厂。整个脚本的网络能力都靠它,不直接用 telnetlib/paramiko
from netmiko import ConnectHandler #
#2.连接参数:连谁?怎么连
DEVICE = {
"device_type": "hp_comware_telnet",
"host": "192.168.100.161", # R1
"port": 23,
"username": "R1",
"password": "Admin@12345",
"fast_cli": True,
"session_log": "p0201ftp.log", # 排错用
}
#3. 业务参数:用户名和口令
FTP_USER = "R1"
FTP_PWD = "Admin@12345"
#4. 命令清单。如果要当"翘脚老板"以就得把这些参数打包传给R1
CONFIG = [
"ftp server enable",
f"local-user {FTP_USER} class manage",
f" password simple {FTP_PWD}",
# 这台设备的 service-type 一次只吃一个关键字,
# 写成 "service-type ftp ssh telnet" 会报 Too many parameters,所以拆开写
" service-type ftp",
" service-type ssh",
" service-type telnet",
" authorization-attribute user-role network-admin",
" authorization-attribute work-directory flash:/",
" quit",
]
#5.主流程 怎么这些:先连接,打包送命令,保存,验证,断开连接释放资源。
def main() -> None:
conn = ConnectHandler(**DEVICE)
try:
print("提示符:", conn.find_prompt())
print("\n===== 下发配置 =====")
print(conn.send_config_set(CONFIG))
print("\n===== 保存 =====")
print(conn.send_command("save force", read_timeout=180))
print("\n===== 验证 =====")
print(conn.send_command("display current-configuration | include ftp"))
print(conn.send_command("display local-user"))
finally:
conn.disconnect()
if __name__ == "__main__":
main()
运行:
powershell
(venv) PS F:\workbuddywork\h3hcl\hcl-lab\scripts> python .\p0201ftp.py
提示符: <R1>
===== 下发配置 =====
system-view
System View: return to User View with Ctrl+Z.
[R1]ftp server enable
[R1]local-user R1 class manage
[R1-luser-manage-R1] password simple Admin@12345
[R1-luser-manage-R1] service-type ftp
[R1-luser-manage-R1] service-type ssh
[R1-luser-manage-R1] service-type telnet
[R1-luser-manage-R1] authorization-attribute user-role network-admin
[R1-luser-manage-R1] authorization-attribute work-directory flash:/
[R1-luser-manage-R1] quit
[R1]return
<R1>
===== 保存 =====
Validating file. Please wait...
Configuration is saved to device successfully.
===== 验证 =====
service-type ftp
ftp server enable
Device management user R1:
State: Active
Service type: FTP/SSH/Telnet
User group: system
Bind attributes:
Authorization attributes:
Work directory: flash:/
User role list: network-admin, network-operator
Password control configurations:
Password complexity: username checking
Device management user test:
State: Active
Service type: None
User group: system
Bind attributes:
Authorization attributes:
Work directory: flash:
User role list: network-operator
Password control configurations:
Password complexity: username checking
Total 2 local users matched.
如果想省事,建一个登录脚本ftp.txt
open 192.168.100.161
R1
Admin@12345
dir
登录:
cmd
(venv) PS F:\workbuddywork\h3hcl\hcl-lab\scripts> ftp -s:ftp.txt
ftp> open 192.168.100.161
连接到 192.168.100.161。
220 FTP service ready.
502 Command not implemented.
用户(192.168.100.161:(none)):
331 Password required for R1.
230 User logged in.
ftp> dir
200 PORT command successful
150 Connecting to port 64943
drwxrwxrwx 2 0 0 4096 Oct 28 11:37 diagfile
-rwxrwxrwx 1 0 0 735 Oct 28 12:44 hostkey
-rwxrwxrwx 1 0 0 252 Oct 29 10:40 ifindex.dat
-rwxrwxrwx 1 0 0 43136 Oct 28 11:37 licbackup
-rwxrwxrwx 1 0 0 43136 Oct 28 11:37 licnormal
drwxrwxrwx 2 0 0 4096 Oct 28 11:37 logfile
-rwxrwxrwx 1 0 0 0 Oct 28 11:37 msr36-cmw710-boot-r0424p22.bin
-rwxrwxrwx 1 0 0 0 Oct 28 11:37 msr36-cmw710-system-r0424p22.bin
drwxrwxrwx 2 0 0 4096 Oct 28 11:37 seclog
-rwxrwxrwx 1 0 0 591 Oct 28 12:44 serverkey
-rwxrwxrwx 1 0 0 3014 Oct 29 10:40 startup.cfg
-rwxrwxrwx 1 0 0 103666 Oct 29 10:40 startup.mdb
226 12 matches total
ftp: 收到 921 字节,用时 0.01秒 61.40千字节/秒。
本节小结:
1.导入-->2.指定连接参数-->3.指定你要配置服务的业务参数-->4.打包送过去的配置的命令集合-->5.定义你的动作流程(下发执行、保存、验证)-->6.main生效
ct 29 10:40 startup.cfg
-rwxrwxrwx 1 0 0 103666 Oct 29 10:40 startup.mdb
226 12 matches total
ftp: 收到 921 字节,用时 0.01秒 61.40千字节/秒。
### 本节小结:
1.导入-->2.指定连接参数-->3.指定你要配置服务的业务参数-->4.打包送过去的配置的命令集合-->5.定义你的动作流程(下发执行、保存、验证)-->6.main生效