设为首页 收藏本站
查看: 8393|回复: 0

[经验分享] python多线程自动备份华为H3C交换机配置和LOG

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-12-2 08:51:23 | 显示全部楼层 |阅读模式
之前试过用expect结合bash脚本备份交换机LOG,但由于是串行执行,设备很多的情况下耗时太长,而且经常出错导致备份不完整。于是在网上找python多线程处理的相关文章,但基本都是基于tftp备份当时运行的配置文件,不能根据自定义巡检命令取得返回结果,我想要的是类似SECURECRT下用.vbs脚本备份的效果,所以根据网上一些例子做了这个备份脚本。由于是多线程执行,所以执行时长决定于最多配置的那台设备的命令运行时长。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[iyunv@localhost shell]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[iyunv@localhost shell]# python --version
Python 2.7.5
[iyunv@localhost shell]#  tree /networkbackup/
/networkbackup/
|-- log
|   `-- 10.06.99.01_2016-12-01_00:00:01.log
`-- shell
    |-- command.txt
    |-- main.py
    `-- sw.txt   
[iyunv@localhost shell]# pwd
/networkbackup/shell
[iyunv@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脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
[iyunv@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的文件名对齐显示
1
2
3
4
5
6
7
8
9
10
11
12
[iyunv@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





#交换机巡检命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[iyunv@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时自动备份
1
2
[iyunv@localhost shell]# crontab -l
0 0 * * * /usr/bin/python /networkbackup/shell/main.py >/dev/null 2>&1




#备份效果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[iyunv@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




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



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-308464-1-1.html 上篇帖子: windows下python安装scrapy碰到的坑 下篇帖子: python编译安装 交换机配置 python 多线程 华为
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表