Python multi-threaded automatic backup Huawei H3C switch configuration and log

Source: Internet
Author: User

Previously tried to back up switch log with expect with bash script, but because it was serially executed, the device took too long and often made a mistake, resulting in incomplete backups. So on the internet to find Python multi-threading related articles, but the basic is based on the TFTP backup run at the time of the configuration file, not according to the Custom Patrol command to obtain the return results, What I want is the effect of using a. vbs script backup like SECURECRT, so this backup script is done based on some examples on the web. Because it is multithreaded execution, the execution time is determined by the length of the command run for the most configured device.


[[email protected] shell]# cat /etc/redhat-release  CentOS Linux release 7.2.1511  (Core)  [[email protected] shell]#  Python --versionpython 2.7.5[[email protected] shell]#  tree /networkbackup networkbackup/|-- log|    '-- 10.06.99.01_2016-12-01_00:00:01.log '-- shell     |-- command.txt    |-- main.py     '- - sw.txt   [[email protected] shell]# pwd/networkbackup/shell[[email  Protected] shell]# ll Total dosage  12-rw------- 1 root root  380 11 month   28 13:01 COMMAND.TXT-RW------- 1 root root 1975 11 Month  28 13:26  MAIN.PY-RW------- 1 root root  337 11 Month  28 14:08 sw.txt 



#python脚本

[[Email protected] shell]#  cat main.py#!/usr/bin/env python#coding:utf-8import  sysimport osimport telnetlibimport timecimport threadingimport datetimenow  = datetime.datetime.now () #Use  for loop to telnet into each  Routers and execute commandsclass 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)    &nbsP;        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 with 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     #设置延时, Give the following command enough time to get the return value and adjust it to the appropriate length             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             returndef 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文件, the completion of 0 is to create a log file name alignment display

[email protected] shell]# cat sw.txt10.06.99.0110.06.99.1110.06.99.1210.06.99.1310.06.99.1410.06.99.1510.06.99.1610.06.99.1710.06.99.1810.06.99.1910.06 .99.20



#交换机巡检命令

[email protected] shell]# cat Command.txtdisplay currentdisplay interface briefdisplay IP interface briefdisplay arpdisp Lay Mac-addressdisplay trapbufferdisplay Alarm Alldisplay interface desdisplay ACL alldisplay cpudisplay Memory-usagedisplay healthdisplay vrrpdisplay devicedisplay powerdisplay IP Routing statisticsdisplay versiondisplay Elabeldisplay Interfacedisplay Logbuffer



#设置crontab每天0时自动备份

[[email protected] shell]# crontab-l0 0 * * */usr/bin/python/networkbackup/shell/main.py >/dev/null 2>&1


#备份效果:

[[email protected] log]#         ll *10.06.99*  -lrt-rw-r--r-- 1 root root 139164 11 Month  30 00:08 10.06.99.49_ 2016-11-30_00:00:01.log-rw-r--r-- 1 root root 227535 11 Month  30 00:08  10.06.99.19_2016-11-30_00:00:01.log-rw-r--r-- 1 root root 254217 11 Month  30  00:08 10.06.99.18_2016-11-30_00:00:01.log-rw-r--r-- 1 root root 265829 11 month   30 00:08 10.06.99.11_2016-11-30_00:00:01.log-rw-r--r-- 1 root root 279509  November  30 00:08 10.06.99.14_2016-11-30_00:00:01.log-rw-r--r-- 1 root root  290337 11 Month  30 00:08 10.06.99.16_2016-11-30_00:00:01.log-rw-r--r-- 1 root  Root 290419 11 Month  30 00:08 10.06.99.12_2016-11-30_00:00:01.log-rw-r--r-- 1  Root root 291343 11 Month 30 00:08 10.06.99.13_2016-11-30_00:00:01.log-rw-r--r-- 1 root root 291172  11 Month  30 00:08 10.06.99.20_2016-11-30_00:00:01.log-rw-r--r-- 1 root root  315611 11 Month  30 00:08 10.06.99.15_2016-11-30_00:00:01.log-rw-r--r-- 1 root  root 297378 11 Month  30 00:08 10.06.99.17_2016-11-30_00:00:01.log-rw-r--r-- 1  root root 374233 11 month  30 00:08 10.06.99.01_2016-11-30_00:00:01. Log-rw-r--r-- 1 root root 140028 12 Month   1 00:08 10.06.99.49_ 2016-12-01_00:00:01.log-rw-r--r-- 1 root root 226886 12 Month   1 00:08  10.06.99.19_2016-12-01_00:00:01.log-rw-r--r-- 1 root root 266558 12 month    1 00:08 10.06.99.11_2016-12-01_00:00:01.log-rw-r--r-- 1 root root 253893  12 Month   1 00:08 10.06.99.18_2016-12-01_00:00:01.log-rw-r--r-- 1 root root 278817 12 Month   1 00:08  10.06.99.14_2016-12-01_00:00:01.log-rw-r--r-- 1 root root 291238 12 month    1 00:08 10.06.99.20_2016-12-01_00:00:01.log-rw-r--r-- 1 root root 290908  12 Month   1 00:08 10.06.99.16_2016-12-01_00:00:01.log-rw-r--r-- 1 root  Root 289772 12 Month   1 00:08 10.06.99.12_2016-12-01_00:00:01.log-rw-r--r-- 1  root root 291625 12 month   1 00:08 10.06.99.13_2016-12-01_00:00:01. Log-rw-r--r-- 1 root root 300205 12 Month   1 00:08 10.06.99.17_ 2016-12-01_00:00:01.log-rw-r--r-- 1 root root 316335 12 Month   1 00:08  10.06.99.15_2016-12-01_00:00:01.log-rw-r--r-- 1 root root 377295 12 month    1 00:08 10.06.99.01_2016-12-01_00:00:01.log 


Cisco devices because for a two password, you can modify the script to pass more than one password parameter, and change the corresponding command to Cisco's command.


This article is from the "Speed of Light" blog, be sure to keep this source http://youxianguang.blog.51cto.com/12081522/1878405

Python multi-threaded automatic backup Huawei H3C switch configuration and log

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.