使用python一键进行批量设备备份
作为一名网工,有时候面对着大量的网络设备,设备备份工作是件很麻烦的事情。
以下是本人亲测成功的脚本代码,一键进行设备备份
脚本如下:
# -*- coding: gbk -*- # 制作人:Tim # 日期:2020-1-2 import telnetlib import time import re import getpass username = input('请输入你的账号:') passwd = getpass.getpass('请输入你的账号密码:') commands = ['term len 0', 'show run', 'show mac add','show cdp nei'] timestr1 = time.strftime('%Y-%m-%d %X', time.localtime()) timestr = time.strftime('%Y-%m-%d', time.localtime()) def main(host): tn = telnetlib.Telnet(host, port=23, timeout=3) time.sleep(2) #tn.set_debuglevel(2) tn.read_until(b"Username:", timeout=3) tn.write(username.encode('ascii') + b'\n') tn.read_until(b"Password:") tn.write(passwd.encode('ascii') + b'\n') print('己成功登录%s [%s]' % (host, timestr1)) time.sleep(0.5) tn.read_until(b"#") hostname_command = 'show run | in hostname' tn.write(hostname_command.encode('ascii') + b'\n') time.sleep(1) cmd_text = tn.read_very_eager().decode('ascii') match = re.split(' ', cmd_text) match1 = match[-1].replace('\n', ' ').replace('\r', '').replace('#', '') hostlist = re.split(' ', match1) hostname = hostlist[1] print(hostname) command_result = tn.read_very_eager().decode('ascii') filepath = 'D:\\SWbackup\\log\\' filename = ('%s %s %s' % (host, hostname, timestr)) print(filename) save = open(filepath + filename + '.txt', 'a', newline='') # 以写的方式来打开文件 print(command_result, file=save) tn.close() def do_work(host_list, all_num): num = 0 for host in host_list: try: main(host) num += 1 print("\r备份进度为:%.2f %%" % (num * 100 / all_num), end="") print('') print("-" * 50) except: f2 = open(r'D:\SWbackup\error.txt', 'a+') print('Can not connect to Device') print('%s is error' % host, file=f2) print('*' * 20 + '%s' % timestr1 + '*' * 20, file=f2) num += 1 print("\r备份进度为:%.2f %%" % (num * 100 / all_num), end="") print('') print("-" * 50) f2.close() host_list = [] f1 = open(r'D:\SWbackup\sw_telnet.txt', 'r') # 获取文件中的每行IP for line in f1.readlines(): line = line.replace('\n', '') host_list.append(line) f1.close print(host_list) all_num = len(host_list) do_work(host_list, all_num)
sw_telnet写上需要备份的设备IP
哪台设备备份失败记录下来
自动生成的备份文件
PS:本人非专业的程序员,如写得不好请多担待,也欢迎留言评论,谢谢
作者:tim54252
如果您看了本篇博客,觉得对您有所收获,请点击右下角的 [推荐]
如果您想转载本博客,请注明出处
如果您对本文有意见或者建议,欢迎留言
感谢您的阅读,请关注我的后续博客