python多執行緒自動備份華為H3C交換
- 2020 年 1 月 9 日
- 筆記
之前試過用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("%sn" %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
思科設備因為為用兩個密碼,可以修改腳本傳遞多一個密碼的參數,並將相應命令改成思科的命令。