原来用expect写了一个备份交换机配置的脚本,但经常是出错或者是备份不成功
现在用python重写过,测试了下,三十多台交换机,总共备份时间也就一秒多,也比原来的脚本稳定多了
再另外说明下:
sw.txt文件中就是需要备份的交换机的IP,每行一个IP就行了
另外需要自己建立一个tftp服务器,交换机配置是备份到tftp服务器上,然后再对配置文件打包等动作
-
#!/usr/bin/env python
-
#coding:utf-8
-
-
import sys
-
import os
-
import telnetlib
-
import time
-
import threading
-
import datetime
-
-
#Use for loop to telnet into each routers and execute commands
-
class Bakconf(threading.Thread):
-
def __init__(self,host,upwd,epwd):
-
threading.Thread.__init__(self)
-
self.host=host
-
self.upwd=upwd
-
self.epwd=epwd
-
-
def run(self):
-
try:
-
tn = telnetlib.Telnet(self.host,port=23,timeout=5)
-
except:
-
print "Can't connection %s"%self.host
-
return
-
tn.set_debuglevel(5)
-
tn.write(self.upwd +b"\n")
-
tn.write("en\n")
-
tn.write(self.epwd + b"\n")
-
tn.write("copy startup-config tftp:\n")
-
tn.write(tftpser + b"\n")
-
tn.write(b"\n")
-
time.sleep(1)
-
tn.write("exit\n")
-
tn.close()
-
-
def main():
-
#user_exec_mode_password
-
upwd1 = "**********"
-
upwd2 = "**********"
-
#privilege_exec_mode_password
-
epwd1 = "**********"
-
epwd2 = "**********"
-
global tftpser
-
tftpser="192.168.103.71"
-
-
for host in open(r'/backup/shell/sw.txt').readlines():
-
dsthost = host.strip('\n')
-
bakconf=Bakconf(dsthost, upwd1, epwd1)
-
bakconf.start()
-
-
hostlist2=["10.1.11.237","10.1.11.238","192.168.103.239"]
-
for host in hostlist2:
-
bakconf=Bakconf(host, upwd2, epwd2)
-
bakconf.start()
-
-
#Backup switch config and tar
-
time.sleep(1)
-
dtime=datetime.datetime.now().strftime("%Y%m%d%H%M%S")
-
os.popen('tar -cjf /backup/cisco/switch-'+dtime+'.tar.bz2 '+ '/tftproot')
-
os.popen('rm -fr /tftproot/*')
-
os.popen('find /backup/cisco/ -mtime +90 -exec rm {} \;')
-
-
if __name__=="__main__":
-
main()
阅读(551) | 评论(0) | 转发(0) |