日历存档: 2018 年 4 月 9 日

使用Python批量备份Cisco和H3C交换机配置到TFTP服务器

分类:网络技术日期:2018-04-09 - 19:23:01评论:6条作者:老谢

需求说明

  由于设备量较大,如何有效的备份交换机设备配置文件就成了问题,经过一番搜索和对比,最终决定使用上传配置文件到tftp的方式进行备份,下面的脚本使用telnetlib库进行操作,感谢大D牛倾情技术支持,再次谢过大D神犇。

修改说明

  Guge上传的V3.0源码仅支持Cisco的交换机,在大D牛的技术支持下,增加了H3C的支持(部分老版本的H3C可能会备份失败),同时也会按照时间自动分目录进行备份,下一步的想法会加入json的支持以及GUI。

main.py V4.0(测试环境Python 2.7.14)
#!/usr/bin/python
#by Kuhn 2014-03-29
#Updata by Derek.s && Jason 2018-04-09
 
import sys
import os
import telnetlib
import getpass
import datetime
import pexpect
import re
 
host = ""
user = "123"
password = "123"
#password = "123"
enpw = "123"
h3cpw = "123"
tftpserver = "8.8.8.8"
now = datetime.datetime.now()
 
def main():
    for host in open("sw.txt", "r").readlines(): 
        dsthost = host.strip('\n')
        try:
            tn = telnetlib.Telnet(dsthost,port=23,timeout=10)
        except:
            print "Can't connection %s"%dsthost
            continue
        #tn.read_until("Username:")
        #tn.write(user+"\n")
        try:
            tn.read_until("Password:")
            tn.write(password+"\n")
            result = tn.read_some()
            rex_h3c_bin_1 = r'%Wrong password'
            login_Failed_H3C_1 = re.search(rex_h3c_bin_1, result)
            rex_h3c_bin_2 = r'%Username or password is invalid.'
            login_Failed_H3C_2 = re.search(rex_h3c_bin_2, result)
        except:
            print "Connection error %s"%dsthost
            continue
        #print(login_Failed_H3C_1, login_Failed_H3C_2)
        if((login_Failed_H3C_1 is None) and (login_Failed_H3C_2 is None)):
            #print("cisco")
            try:
                tn.write("en\n")
                tn.read_until("Password:")
                tn.write(enpw+"\n")
                tn.read_until("#")
                tn.write("copy running-config tftp:\n")
                tn.write(tftpserver+"\n")
                tn.write(now.strftime("%Y/%m/%d")+"/"+host+"\n")
                tn.read_until("#")
                tn.close
                print now.strftime("%Y/%m/%d") + " " + dsthost + " Backup successful."
            except:
                print "Connection error %s"%dsthost
                continue
        else:
            #print("H3c")
            try:
                tn.write(h3cpw+"\n")
                tn.read_until(">")
                tn.write("tftp "+tftpserver+" put flash:/startup.cfg"+" "+now.strftime("%Y/%m/%d")+"/"+host+"\n")
                tn.read_until(">")
                tn.close
                print now.strftime("%Y/%m/%d") + " " + dsthost + " Backup successful(h3c)."
            except:
                print "Connection error %s"%dsthost
                continue
 
if __name__=="__main__":
    main()

参考:http://mybeibei.net/157.html