发布时间:2019-09-22 07:49:16编辑:auto阅读(1807)
之前试过用expect结合bash脚本备份交换机LOG,但由于是串行执行,设备很多的情况下耗时太长,而且经常出错导致备份不完整。于是在网上找python多线程处理的相关文章,但基本都是基于tftp备份当时运行的配置文件,不能根据自定义巡检命令取得返回结果,我想要的是类似SECURECRT下用.vbs脚本备份的效果,所以根据网上一些例子做了这个备份脚本。由于是多线程执行,所以执行时长决定于最多配置的那台设备的命令运行时长。
[root@localhost shell]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@localhost shell]# python --version Python 2.7.5 [root@localhost shell]# tree /networkbackup/ /networkbackup/ |-- log | `-- 10.06.99.01_2016-12-01_00:00:01.log `-- shell |-- command.txt |-- main.py `-- sw.txt [root@localhost shell]# pwd /networkbackup/shell [root@localhost shell]# ll 总用量 12 -rw------- 1 root root 380 11月 28 13:01 command.txt -rw------- 1 root root 1975 11月 28 13:26 main.py -rw------- 1 root root 337 11月 28 14:08 sw.txt
#python脚本
[root@localhost shell]# cat main.py
#!/usr/bin/env python
#coding:utf-8
import sys
import os
import telnetlib
import timec
import threading
import datetime
now = datetime.datetime.now()
#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:
tn = telnetlib.Telnet(self.host,port=23,timeout=5)
tn.set_debuglevel(5)
tn.read_until(b"Username:", timeout=2)
tn.write(self.USERNAME +b"\n")
tn.read_until(b"Password:", timeout=2)
tn.write(self.PASSWORD +b"\n")
tn.write(b"\n")
time.sleep(1)
tn.write("system-view"+"\n")
tn.write("user-interface vty 0 4"+"\n")
tn.write("screen-length 0"+"\n") #设置华为交换机命令不分布显示 cisco用terminal length 0
tn.write("quit"+"\n")
tn.write("quit"+"\n")
#######executive command in the txt file########
for COMMANDS in open(r'/networkbackup/shell/command.txt').readlines():
COMMAND = COMMANDS.strip('\n')
tn.write("%s\n" %COMMAND)
#######executive command in the txt file########
time.sleep(60) #设置延时,使下面命令有足够时间获取返回值,调整到适当时长
output = tn.read_very_eager() #获取返回值
tn.write("quit"+"\n")
filename = "/networkbackup/log/%s_%i-%.2i-%.2i_%.2i:%.2i:%.2i.log" % (self.host,now.year,now.month,now.day,now.hour,now.minute,now.second) #格式化文件名
time.sleep(.1)
fp = open(filename,"w")
fp.write(output)
fp.close()
except:
print "Can't connection %s"%self.host
return
def main():
USERNAME = "xxxxxxxxxxxxx" #交换机登录用户
PASSWORD = "xxxxxxxxxxxxx" #交换机登录密码
for host in open(r'/networkbackup/shell/sw.txt').readlines():
dsthost = host.strip('\n')
bakconf=Bakconf(dsthost, USERNAME, PASSWORD)
bakconf.start()
if __name__=="__main__":
main()#交换机IP文件,补全0是为了生成log的文件名对齐显示
[root@localhost shell]# cat sw.txt 10.06.99.01 10.06.99.11 10.06.99.12 10.06.99.13 10.06.99.14 10.06.99.15 10.06.99.16 10.06.99.17 10.06.99.18 10.06.99.19 10.06.99.20
#交换机巡检命令
[root@localhost shell]# cat command.txt display current display interface brief display ip interface brief display arp display mac-address display trapbuffer display alarm all display interface des display acl all display cpu display memory-usage display health display vrrp display device display power display ip routing statistics display version display elabel display interface display logbuffer
#设置crontab每天0时自动备份
[root@localhost shell]# crontab -l 0 0 * * * /usr/bin/python /networkbackup/shell/main.py >/dev/null 2>&1
#备份效果:
[root@localhost log]# ll *10.06.99* -lrt -rw-r--r-- 1 root root 139164 11月 30 00:08 10.06.99.49_2016-11-30_00:00:01.log -rw-r--r-- 1 root root 227535 11月 30 00:08 10.06.99.19_2016-11-30_00:00:01.log -rw-r--r-- 1 root root 254217 11月 30 00:08 10.06.99.18_2016-11-30_00:00:01.log -rw-r--r-- 1 root root 265829 11月 30 00:08 10.06.99.11_2016-11-30_00:00:01.log -rw-r--r-- 1 root root 279509 11月 30 00:08 10.06.99.14_2016-11-30_00:00:01.log -rw-r--r-- 1 root root 290337 11月 30 00:08 10.06.99.16_2016-11-30_00:00:01.log -rw-r--r-- 1 root root 290419 11月 30 00:08 10.06.99.12_2016-11-30_00:00:01.log -rw-r--r-- 1 root root 291343 11月 30 00:08 10.06.99.13_2016-11-30_00:00:01.log -rw-r--r-- 1 root root 291172 11月 30 00:08 10.06.99.20_2016-11-30_00:00:01.log -rw-r--r-- 1 root root 315611 11月 30 00:08 10.06.99.15_2016-11-30_00:00:01.log -rw-r--r-- 1 root root 297378 11月 30 00:08 10.06.99.17_2016-11-30_00:00:01.log -rw-r--r-- 1 root root 374233 11月 30 00:08 10.06.99.01_2016-11-30_00:00:01.log -rw-r--r-- 1 root root 140028 12月 1 00:08 10.06.99.49_2016-12-01_00:00:01.log -rw-r--r-- 1 root root 226886 12月 1 00:08 10.06.99.19_2016-12-01_00:00:01.log -rw-r--r-- 1 root root 266558 12月 1 00:08 10.06.99.11_2016-12-01_00:00:01.log -rw-r--r-- 1 root root 253893 12月 1 00:08 10.06.99.18_2016-12-01_00:00:01.log -rw-r--r-- 1 root root 278817 12月 1 00:08 10.06.99.14_2016-12-01_00:00:01.log -rw-r--r-- 1 root root 291238 12月 1 00:08 10.06.99.20_2016-12-01_00:00:01.log -rw-r--r-- 1 root root 290908 12月 1 00:08 10.06.99.16_2016-12-01_00:00:01.log -rw-r--r-- 1 root root 289772 12月 1 00:08 10.06.99.12_2016-12-01_00:00:01.log -rw-r--r-- 1 root root 291625 12月 1 00:08 10.06.99.13_2016-12-01_00:00:01.log -rw-r--r-- 1 root root 300205 12月 1 00:08 10.06.99.17_2016-12-01_00:00:01.log -rw-r--r-- 1 root root 316335 12月 1 00:08 10.06.99.15_2016-12-01_00:00:01.log -rw-r--r-- 1 root root 377295 12月 1 00:08 10.06.99.01_2016-12-01_00:00:01.log
思科设备因为为用两个密码,可以修改脚本传递多一个密码的参数,并将相应命令改成思科的命令。
上一篇: 常用的Python库
下一篇: python写报警程序中的声音实现win
51156
50563
41180
38013
32477
29379
28243
23097
23061
21384
1435°
2140°
1778°
1699°
2004°
1775°
2452°
4129°
3996°
2852°