-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_async.py
95 lines (83 loc) · 3.08 KB
/
test_async.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/python
# encoding:utf-8
from multiprocessing import Pool
from login_device import *
import traceback
import time
import os
def h3c_6800(ip):
# 使用生成器功能调用h3c的登录方法登录设备
@devopstools.h3c_login_device(ip, 720)
# show_ip_interf函数中定义要执行的命令及反馈结果
def show_ip_interf():
try:
starttime = time.asctime(time.localtime(time.time()))
print('h3c_6800 login start from %s' % starttime)
# 登陆设备后,执行命令display current-configuration
output = devopstools.h3c_cli('dis cu')
endtime = time.asctime(time.localtime(time.time()))
print('h3c_6800 login end to %s' % endtime)
except Exception:
# 如果出错打印出错原因
print('Some errors occurred when execute command on %s.' % ip)
print(traceback.format_exc())
show_ip_interf()
def cisco_3560(ip):
@devopstools.cisco_login_device(ip, 720)
def show_ip_interf():
try:
starttime = time.asctime(time.localtime(time.time()))
print('cisco_3560 login start from %s' % starttime)
output = devopstools.cisco_cli('show run')
endtime = time.asctime(time.localtime(time.time()))
print('cisco_3560 login end to %s' % endtime)
except Exception:
print('Some errors occurred when execute command on %s.' % ip)
print(traceback.format_exc())
show_ip_interf()
def huawei_6865(ip):
@devopstools.huawei_login_device(ip, 720)
def show_ip_interf():
try:
starttime = time.asctime(time.localtime(time.time()))
print('huawei_6865 login start from %s' % starttime)
output = devopstools.huawei_cli('dis cu')
endtime = time.asctime(time.localtime(time.time()))
print('huawei_6865 login end to %s' % endtime)
except Exception:
print('Some errors occurred when execute command on %s.' % ip)
print(traceback.format_exc())
show_ip_interf()
# 添加3台交换机基础信息,包括ip、产商名称、型号等,正式环境请使用数据库
device_list = [
{
'ip' : '10.110.1.254',
'vendor' : 'h3c',
'model' : 'S6800'
},
{
'ip' : '10.73.254.48',
'vendor' : 'cisco',
'model' : 'C3560'
},
{
'ip' : '10.100.1.15',
'vendor' : 'huawei',
'model' : 'CE6865'
}]
def main(devicelist):
print('Parent process id is %s' % os.getpid())
p = Pool(20)
for device in devicelist:
if device['vendor'] == 'h3c' and device['model'] == 'S6800':
p.apply_async(h3c_6800, args=(device['ip'], ))
elif device['vendor'] == 'cisco' and device['model'] == 'C3560':
p.apply_async(cisco_3560, args=(device['ip'], ))
elif device['vendor'] == 'huawei' and device['model'] == 'CE6865':
p.apply_async(huawei_6865, args=(device['ip'], ))
else:
pass
p.close()
p.join()
if __name__ == '__main__':
main(device_list)