-
Notifications
You must be signed in to change notification settings - Fork 3
/
mod_reboot_switch.py
84 lines (74 loc) · 2.68 KB
/
mod_reboot_switch.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
# encoding: utf-8
import telnetlib
import time
import traceback
from mod_snmp import SnmpSet
from Config import SWITCH_PASSWORD
'''
该模块用于重启交换机
用法:reboot_switches(ips)
ips为需重启交换机IP的list,如["172.16.101.1","172.16.101.2"]
无返回值。
'''
def reboot_switch_telnet(ip):
try:
# 连接Telnet服务器
print('Connecting', ip, '...')
tn = telnetlib.Telnet(ip, port=23, timeout=2)
# tn.set_debuglevel(2)
a = time.time()
# 输入登录密码
print('Connected. Logining...')
tn.read_until('assword:'.encode('gbk'), 5)
tn.write(SWITCH_PASSWORD.encode('gbk') + b'\n')
# 登录完毕后执行命令
tn.read_until('>'.encode('gbk'), 5)
print('Login succeed! Rebooting...')
tn.write("reboot\n".encode('gbk'))
a = tn.read_until("[Y/N]".encode('gbk'), 60)
print(a)
tn.write("y\n".encode('gbk'))
print('Send "y"')
if a.decode('GBK').find('This command will reboot the device') != -1:
a = tn.read_until("):".encode('gbk'), 60)
print(a)
if a.decode == ':y':
tn.close()
print('reboot succeed!')
return
tn.write("\n".encode('gbk'))
print('Send "\\n"')
print(tn.read_until("[Y/N]".encode('gbk'), 60))
tn.write("y\n".encode('gbk'))
print('Send "y"')
print(tn.read_until("[Y/N]".encode('gbk'), 60))
tn.write("y\n".encode('gbk'))
print('Send "y"')
# 执行完毕后关闭连接
tn.close()
print('reboot succeed!')
except:
a = traceback.format_exc()
print(a[a.find('Error:') + 7:])
def reboot_switch_snmp(ip):
a = SnmpSet(ip, 'S2700', "reboot") # E152B不支持SNMP重启(我没找到MIB节点)
print("重启交换机", ip, a)
return 0
def reboot_switches(ips):
for ip in ips:
reboot_switch_snmp(ip)
# reboot_switch_telnet(ip)
'''
<D2_4F_H2_E152B_1>reboot
Start to check configuration with next startup configuration file, please wait.........DONE!
This command will reboot the device. Current configuration will be lost, save current configuration? [Y/N]:y
Please input the file name(*.cfg)[flash:/config.cfg]
(To leave the existing filename unchanged, press the enter key):
flash:/config.cfg exists, overwrite? [Y/N]:y
Validating file. Please wait....
The current configuration is saved to the active main board successfully.
Configuration is saved to device successfully.
This command will reboot the device. Continue? [Y/N]:y
<X1_4F_H3_S3050_4>reboot
This will reboot Switch. Continue? [Y/N] y
'''