300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > python自动备份交换机配置脚本_python代码自动备份交换机配置

python自动备份交换机配置脚本_python代码自动备份交换机配置

时间:2023-03-22 12:11:16

相关推荐

python自动备份交换机配置脚本_python代码自动备份交换机配置

参考文献

参考文献1:/code/snippet_7933_47935

测试代码

前言:这个程序用来批量备份博达交换机的配置,需要在桌面打开tftpd32.exe,还有打开sw.txt,

txt里面一行写一个ip地址,同时修改代码对应的tftpser ip地址。这个代码是改写现成的代码,

还有线程部分不太懂,是可以多线程同时进行的。

#!/usr/bin/python

# -*- coding: utf-8

import sys,os,telnetlib,time,threading,datetime,logging,re

import msvcrt #这个用来定义退出cmd,不然程序一闪而过

#---------------------------这段程序用来定义日志------------------------------

logger = logging.getLogger("Test") #为这个log取个名字

logger.setLevel(logging.DEBUG) #定义debug(优先级10)以上的告警都进行捕获

fh = logging.FileHandler("test.log") #把log写入相应的文件中

fh.setLevel(logging.DEBUG) #定义debug(优先级10)以上的告警都写入到文件

sh = logging.StreamHandler() #把log打印到屏幕上

sh.setLevel(logging.DEBUG) #定义debug(优先级10)以上的告警都打印到屏幕上

#定义要打印的参数的格式(参数很多),系统时间,name(logging.getLogger("Test")),优先级,自定义日志

formatter = logging.Formatter("%(asctime)s [%(name)s] -

[%(levelname)s]: %(message)s")

fh.setFormatter(formatter) #把格式formatter赋值给fh

sh.setFormatter(formatter) #把格式formatter赋值给sh

logger.addHandler(fh) #把fh加入logger.

logger.addHandler(sh) #把sh加入logger.

#---------------------------这段程序用来备份配置------------------------------

#Use for loop to telnet into each routers and execute

commands

class Bakconf(threading.Thread):

def __init__(self,host,username,password):

threading.Thread.__init__(self)

self.host=host

self.username=username

self.password=password

def run(self):

try:

logger.debug('------------------------connect---------------------------')

tn = telnetlib.Telnet(self.host,port=23,timeout=10)

except:

print "Can't connection %s"% self.host

return

tn.set_debuglevel(5)

logger.debug('-------------------------login----------------------------')

tn.write(self.username +b"\n")

tn.write("en\n")

tn.write(self.password + b"\n")

logger.debug('-----------------------backup

start---------------------------')

tn.write("copy startup-config tftp: %s \n"% tftpser)

tn.write(host + "\n")

#tn.write(b"\n")

time.sleep(1)

tn.write("exit\n")

tn.close()

logger.debug('--------------------------end------------------------------')

def main():

logger.debug('-----------------------read

username+password---------------------------')

username = "admin"

password = "admin"

global tftpser

logger.debug('-------------------read

tftpserverip-----------------------')

tftpser="66.66.66.100"

for host in open(r'sw.txt').readlines():

dsthost = host.strip('\n')

bakconf=Bakconf(dsthost, username, password)

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()

print ("Press 'D' to exit...") #提示press ‘D’ 退出cmd

while True:

if ord(msvcrt.getch()) in [68, 100]:#获取到‘D’键位

break #退出程序

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。