H3C自动配置(HTTP Python方式)

This is an article that was created 551 days ago, and the information may have evolved or changed.

一个小测试,实现HCL模拟器下利用python脚本根据交换机的背板MAC地址(真机环境可以换成序列号)自动加载配置。

环境介绍:

ScreenCaputure230318161432

  • HCL v5.7.1
  • Python 3.10.10
  • Tftpd64 version 4.60

步骤

编写python脚本

制作一个后缀名为.py的脚本。主要作用是空配置交换机开机执行自动配置,下载到本地并执行文件

由于HCL模拟器里的交换机序列号都一样的,勉为其难改用背板MAC地址;如果是真机设备直接用序列号;脚本中预留了序列号的代码,简单修改一下便可

  • 设备 H3C S6850_1: MAC_ADDRESS:68-bf-6d-ad-01-04
  • 设备 H3C S6850_2: MAC_ADDRESS:68-45-dc-f1-02-04
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python
#-*-coding:utf-8-*-

import comware
import platformtools


def do_reboot():
# scheduler job reboot
platformtools.CLI('system-view ;scheduler job startup_reboot ;command 1 reboot force ;quit')
# exec-delay
platformtools.CLI('system-view ;scheduler schedule do_reboot ;job startup_reboot ;time once delay 1')


def apply_sw1_config():
# Specify system startup
platformtools.CLI('restore startup-configuration from 172.31.100.2 sw1_config.cfg')
do_reboot()


def apply_sw2_config():
# Specify system startup
platformtools.CLI('restore startup-configuration from 172.31.100.2 sw2_config.cfg')
do_reboot()

# more device ...
# def apply_sw3_config():
# # Specify system startup
# platformtools.CLI('restore startup-configuration from 172.31.100.2 sw3_config.cfg')
# do_reboot()

# find DEVICE_SERIAL_NUMBER
res = platformtools.CLI('dis dev man', False)

for i in res.get_output():
# if 'DEVICE_SERIAL_NUMBER' in i:
if '68-bf-6d-ad-01-04' in i:
apply_sw1_config()
break
if '68-45-dc-f1-02-04' in i:
apply_sw2_config()
break
# more device ...
# if 'DEVICE_SERIAL_NUMBER' in i:
# apply_sw2_config()
# break

准备设备配置文件

  • sw1_config.cfg
  • sw2_config.cfg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#

sysname sw1_s6850

#

telnet server enable
ssh server enable

#

vlan 10 to 20

#

int vlan 1
ip add 172.31.100.10 24

#

int vlan 10
ip add 192.168.10.254 24

#

interface GigabitEthernet 1/0/23
port link-type trunk
port trunk permit vlan all
undo shutdown

#

interface GigabitEthernet 1/0/24
port link-type trunk
port trunk permit vlan all
undo shutdown

#

local-user admin
password simple Cisco123!@#
authorization-attribute user-role network-admin
authorization-attribute user-role network-operator
service-type ssh telnet
quit

#

line vty 0 63
authentication-mode scheme
user-role network-operator

#

return

启动HTTP服务端

搭建一个简单的http服务端(可以使用网上的各种小工具,安全性自己判断),如果有python环境可以直接启动一个简易的http服务端,搭建完成后使用浏览器打开链接,确保url能正确指向第一步中的python脚本文件。

1
2
PS D:\TEMP\tftpd64.460> python -m http.server
Serving HTTP on :: port 8000 (http://[::]:8000/) ...

浏览器URL测试

ScreenCaputure230318172646

配置TFTP服务端和DHCP服务端参数

  • TFTP目录选择设备配置文件的目录
  • 如果电脑有多块网卡,选择绑定需要用到的网卡IP地址
  • DHCP填写的Boot File 是python脚本文件的URL
ScreenCaputure230318155000 ScreenCaputure230318155038

Tips:为了方便使用建议把device.py和设备.cfg配置文件统一放到TFTP的目录下,http服务端的启动命令也在TFTP目录下启动。

ScreenCaputure230318164825

启动测试

  1. 临时关闭本机环境中的其他DHCP服务端
  2. HCL模拟器里的交换机桥接到本地
  3. 模拟器设备开机

TFTP软件观察发现已经分配两个地址

ScreenCaputure230318161954

第一台S6850 启动,观察发现下载了device.py文件,并按照py脚本命令执行了一些配置

ScreenCaputure230318161917

第二台S6850 启动,观察发现下载了device.py文件,并按照py脚本命令执行了一些配置

ScreenCaputure230318161928

按照设定的1分钟后重启设备,观察到出现两次Press ENTER to get started.表示已经重启了;设备名称也按照配置文件里的更改了

ScreenCaputure230318162531

ScreenCaputure230318162541

使用SSH登陆设备,能通过配置文件里配置的IP地址登陆设备

ScreenCaputure230318162808

ScreenCaputure230318162820

查看sw1的一些配置

ScreenCaputure230318163152

结束

  1. HCL模拟器的序列号一致不能通过序列号操作,实为憾事
  2. 这个方法可应用在一定规模的组网,前提条件是安装前要记录安装位置与设备序列号的对应关系
  3. 这个方法只能应用在完全空配置的设备,保存过配置的交换机就不能操作了
  4. 如果是真实设备使用,注意本机电脑的防火墙设置
  5. 前线安装人员要确定设备已加电和设备端口的指示灯状态
  6. 方法操作没有问题,但是有时候去实施,甲方看到你老是在电脑面前操作,这个体现不出工作量,需要注意,两天的活你半小时不到干完了
  7. 修改一下脚本,还能用在没有console线的环境下应急,同样确保设备是空配置启动
  8. device.py文件里的代码有一个重启设备的定时任务,由于是做实验我设置了1分钟后重启,如果是真实环境这个延时重启建议配置时间大一点
一个使用nornir框架编写的跑脚本工具 观察OSPF虚链路和虚链路的替代办法
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×