使用python2脚本定时批量自动备份H3C交换机配置

使用python2脚本定时批量自动备份H3C交换机配置

王忘杰
2022-06-29 / 0 评论 / 55 阅读 / 正在检测是否收录...

交换机自动备份配置(h3c)
CSDN@willwillwang
https://blog.csdn.net/wq298102526/article/details/108796824

定时计划,每天7点备份

crontab -e
0 7 * * * python2 /root/swbackup.py > /root/swbackup.log

python脚本 swbackup.py
利用telnetlib交互登录查看交换机配置并保存,可修改命令后用于任意品牌交换机

#!/usr/bin/python2
# -*- coding: UTF-8 -*-
import telnetlib
import time
import re
import codecs
import time
import os
now = time.strftime("%y%m%d")
path = "/root/backup/%s"%now
if not os.path.exists(path):
    os.makedirs(path)
Hostall = """192.168.1.1
192.168.1.2
192.168.1.3
192.168.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'passwd\n')
        time.sleep(3)
        tn.write(b'screen-length disable\n')
        tn.write(b'dis cur\n')
        tn.read_some()
        tn.write(b'undo screen-length disable\n')
        tn.write(b'quit\n')
        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:
        print("fail %s"%Host)
0

讨论

根据公安部要求,本站无评论功能,若有问题欢迎加入QQ群讨论,群在侧边栏,刷新几次可见