使用Python批量备份Cisco和H3C交换机配置到TFTP服务器
分类:网络技术日期:2018-04-09 - 19:23:01作者:老谢
需求说明
由于设备量较大,如何有效的备份交换机设备配置文件就成了问题,经过一番搜索和对比,最终决定使用上传配置文件到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() |
非牛求放过
欢迎大D神犇前来指导工作
报错“Can’t connection ”大概会是什么原因呢?用户名、密码、特权密码都是正确的
跑脚本的主机可达交换机么?
二者是可达的,跑脚本的是我自己的笔记本
笔记本可以直接telnet上交换机
tftp服务器也是正常的。我在交换机上通过命令“cpoy running-config tftp:172.16.103.43”上传配置文件到tftp服务器是成功的
我的代码如下,已经转为python3格式:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
import os
import telnetlib
import getpass
import datetime
import pexpect
import re
host = “”
user = “netadmin”
password = “sw434.as”
enpw = “QWE102@.on”
h3cpw = “123”
tftpserver = “172.16.103.43”
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=15)
tn.read_until(“Username:”)
tn.write(user + “\n”)
except:
print(“Can’t connection %s” % dsthost)
continue
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()