Python备份交换机配置

作为一个网络工程师,交换机配置备份还是必要的,如果哪天交换机挂了,或者配置端口配置错了,想知道之前的vlan等这些情况都需要查看之前的配置,于是自己用python写了个备份交换机配置的自动化脚本。

思路

linux服务器定时运行脚本
根据日期创建文件夹并获取配置写入文件

配置

1
2
48 23 * * * python3 /home/python/switchbk.py > /home/python/switchbk-log
#crontabl设置每天23:48运行
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
import telnetlib
import time
import re
import codecs
import time
import os
now = time.strftime("%y%m%d")
#当前日期
path = "/home/python/switchconf/%s"%now
##目录自定义
if not os.path.exists(path):
os.makedirs(path)

Hostall = """172.19.254.10
172.19.254.11
10.1.1.1
10.1.1.2
10.1.1.3
10.1.1.4
10.1.1.5
"""

Hostlist = Hostall.splitlines()
for Host in Hostlist:
try:
tn = telnetlib.Telnet(Host, timeout=15)
time.sleep(3)
tn.write(b'admin\n')
time.sleep(1)
tn.write(b'admin\n')
time.sleep(3)
tn.write(b'dis cur\n')
tn.read_some()
tn.write(b'quit\n')
# print("logout")
mac1 = tn.read_all()
f1 = open('%s/%s'%(path,Host),'wb')
print("open file")
f1.write(mac1)
print("write file")
f1.close()
print ("%s finish"%Host)
except:
80,5 93%
print ("%s finish"%Host)
except:
print("fail %s"%Host)

Python备份交换机配置
http://example.com/2022/09/02/Python备份交换机配置/
作者
WangQiang
发布于
2022年9月2日
许可协议