python多线程自动备份华为H3C交换机配置和LOG

之前试过用expect结合bash脚本备份交换机LOG,但由于是串行执行,设备很多的情况下耗时太长,而且经常出错导致备份不完整。于是在网上找python多线程处理的相关文章,但基本都是基于tftp备份当时运行的配置文件,不能根据自定义巡检命令取得返回结果,我想要的是类似SECURECRT下用.vbs脚本备份的效果,所以根据网上一些例子做了这个备份脚本。由于是多线程执行,所以执行时长决定于最多配置的那台设备的命令运行时长。

[[email protected] shell]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[[email protected] shell]# python --version
Python 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
总用量 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脚本

[[email protected] 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的文件名对齐显示

[[email protected] 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

#交换机巡检命令

[[email protected] 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时自动备份

[[email protected] shell]# crontab -l
0 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月 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

思科设备因为为用两个密码,可以修改脚本传递多一个密码的参数,并将相应命令改成思科的命令。

时间: 2024-08-22 12:39:56

python多线程自动备份华为H3C交换机配置和LOG的相关文章

超级全的H3C交换机配置命令

H3C交换机配置命令大全 1.system-view 进入系统视图模式 2.sysname 为设备命名 3.display current-configuration 当前配置情况 4. language-mode Chinese|English 中英文切换 5.interface Ethernet 1/0/1 进入以太网端口视图 6. port link-type Access|Trunk|Hybrid 设置端口访问模式 7. undo shutdown 打开以太网端口 8. shutdown

H3C交换机配置远程登录

首先交换机会自带console线一根,现在很少有笔记本带串口了,我的TP也不带,于是买了根转换线,这里要说一下,转换线是需要装驱动的,可以把带的驱动装好,最好是copy一份到网盘里. 装好驱动后,可以在设备管理器端口那一栏查查serial to usb的端口COM号,我的本子上提示COM7. 我的是win7系统,不带超级终端,自己用的是SecureCRT,用SecureCRT连console口的时候,这里需要设置一下. 这里flow control三个选项都不要选. SecureCRT是个很好的

华为路由器交换机配置命令大全

华为路由器交换机配置命令:计算机命令 PCAlogin:root;使用root用户 password:linux;口令是linux #shutdown-hnow;关机 #init0;关机 #logout;用户注销 #login;用户登录 #ifconfig;显示IP地址 #ifconfigeth0netmask;设置IP地址 #ifconfigeht0netmaskdown;禁用IP地址 #routeadd0.0.0.0gw;设置网关 #routedel0.0.0.0gw;删除网关 #route

H3C交换机配置策略路由

153网段配置到tfs服务器策略路由 1. 创建 Acl访控列表(客户端到服务器) acl number 31rule 0 permit ip source 172.20.153.0 0.0.0.255 destination 172.20.20.65 0 2. 创建Acl访控列表(服务器到客户端,此策略只需创建一个acl集,不同网段只需添加不同策略条目即可) acl number 3002 rule 1 permit ip source 172.20.20.65 0 destination 1

H3C交换机配置链路聚合

# 创建聚合组1(根据具体情况选择下面两种方式之一). l采用静态聚合模式:创建二层聚合接口1 <SwitchA> system-view [SwitchA] interface bridge-aggregation 1 [SwitchA-Bridge-Aggregation1] quit l采用动态聚合模式:创建二层聚合接口,并配置动态聚合模式 <SwitchA> system-view [SwitchA] interface bridge-aggregation 1 [Swit

Python通过paramiko模块备份H3C交换机配置

1.过程思路 备份配置前,先保存交换机running config到starup config 交换机通过tftp备份配置文件 批量备份交换机配置(通过excel文件保存交换机IP) name ip SUZ-SW-101 10.X.X.1 SUZ-SW-102 10.X.X.2 SUZ-SW-103 10.X.X.3 SUZ-SW-104 10.X.X.4 2.python代码 import xlrd import paramiko import time def ssh_SW(name,ip)

华为3026c交换机配置tftp备份命令

华为3026c交换机ftp备份配置:方法一:iis建ftp服务器,要设置用户名密码(百度经验)前提:先找一台能ping通交换机的电脑,通过iis建ftp服务器.1.查看当前路径下的文件和目录>dir2.连接ftp服务器>ftp 192.168.104.153.输入ftp服务器用户名和密码:bhjftp4.指定ftp传输txt格式为bin:[ftp]bin5.开始上传:[ftp]put vrpcfg.txt 方法二:SolarWinds-TFTP-Server建ftp服务器,设置共享文件路径,其

H3C交换机配置命令(收集)

1:配置登录用户,口令等 <H3C>                   //用户直行模式提示符,用户视图 <H3C>system-view        //进入配置视图 [H3C]                  //配置视图(配置密码后必须输入密码才可进入配置视图) [H3C] sysname xxx //设置主机名成为xxx这里使用修改特权用户密码 <H3C>system-view  [H3C]super password level 3 cipher/sim

H3C交换机配置文件备份方法

 1.首先在一台计算机上运行TFTP Server软件,这里使用的是SolarWinds TFTP Server 8.0,在设置中配置好Root 目录:在安全中配置好文件的传送方向(接收.发送.发送\接收):高级中可以配置允许通过的IP地址段,这里不做配置:之后就可以登录交换机进行配置文件的传送了.  2.通过Telnet登录到SMC交换机,在特权模式下输入如下命令:  <H3C1409FS24-01>tftp172.20.34.2  putconfig.cfg  config.cfg   \