-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMT_update.py
More file actions
186 lines (159 loc) · 5.56 KB
/
MT_update.py
File metadata and controls
186 lines (159 loc) · 5.56 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# -*- coding: gbk -*-
from ftplib import FTP
import re
import os
import telnetlib
import threading # 线程
from multiprocessing.dummy import Pool # 线程
class Update:
def __init__(self):
self.ips = []
# 获取ip地址以及文件名称
def getip(self, i):
path = os.path.join(os.path.expanduser("~"), 'Desktop//')
cwdsend = 'ramdisk'
# 读取配置文件,获取ip地址及终端版本
with open('mt_info.ini', 'r') as f:
info = f.readlines()
ip = re.findall('([\d\.]+)\t.*?', info[i])[0]
sendfilename = re.findall('\t([\w\.\_]+)', info[i])[0]
return ip, cwdsend, sendfilename, path
# 获取文件名
def getfilename(self, ip, cwd):
try:
ftp = FTP()
timeout = 30
port = 21
ftp.connect(ip, port, timeout)
ftp.login('admin', 'admin')
print(ftp.getwelcome())
ftp.cwd(cwd)
print('上传完成后ramdisk中的文件')
list = ftp.nlst()
for name in list:
print(name)
except:
print('%s无法显示文件目录,请检查ip地址是否正确!' % ip)
# ftp登陆终端
def ftp_mt(self, ip, cwd):
ftp = FTP()
timeout = 5
port = 21
ftp.connect(ip, port, timeout)
ftp.login('admin', 'admin')
print(ftp.getwelcome())
ftp.cwd(cwd)
print('%s ramdisk中已有文件如下:' % ip)
list = ftp.nlst()
for name in list:
print(name)
return ftp
# ftp下载文件,从服务器下载文件
def getfile(self,ip, cwd, filename, path):
try:
ftp = self.ftp_mt(ip, cwd)
ftp.retrbinary('RETR '+filename, open(path + filename,'wb').write)
ftp.quit()
except:
print('%s下载文件失败...' % ip)
# 上传文件
def sendfile(self, ip, cwd, sendfilename, path):
flag = 1
try:
ftp = self.ftp_mt(ip, cwd)
print('%s开始上传 ...' % ip)
ftp.storbinary('STOR ' + sendfilename, open(path+sendfilename, 'rb'))
print('%s 升级文件上传成功,正在升级...' % ip)
ftp.quit()
except:
print('%s连接超时...' % ip)
self.ips.append(ip)
flag = 0
return flag
# 调用上传文件、升级函数
def mainfunc(self, ip, cwdsend, sendfilename, path):
flag = self.sendfile(ip, cwdsend, sendfilename, path)
if flag == 1:
self.getfilename(ip, cwdsend)
self.allupdate(ip, sendfilename)
if threading.active_count() == 2:
self.show_failed()
# 多线程实现,不使用动态变量
def the_start(self):
threads = []
print('Welcome!!!')
print('请将升级包全部放置于桌面,并添加好配置文件,以table键作为配置文件间隔!!')
with open('mt_info.ini', 'r') as f:
info = f.readlines()
mtn = len(info)
for i in range(0, mtn):
ip, cwdsend, sendfilename, path = self.getip(i)
t = threading.Thread(target=self.mainfunc, args=(ip, cwdsend, sendfilename, path))
threads.append(t)
for th in threads:
th.start()
th.join()
# 通过multiprocessing.dummy暂时无法实现,无法实现传递多个参数,只能确定一个升级版本,实现多终端升级
def pool_start(self):
args = []
print('Welcome!!!')
mtn = int(input('请输入想要升级的终端个数:'))
for i in range(0, mtn):
ip, cwdsend, sendfilename, path = self.getip(i+1)
t = (ip, cwdsend, sendfilename, path)
print(type(t))
args.append(t)
pool = Pool(8)
pool.map(self.mainfunc, args)
# 执行上传的文件,进行升级
def allupdate(self, ip, sendfilename):
commands = ['she', 'cd ramdisk', 'allupdate %s' % sendfilename, 'reboot']
try:
tn = self.telnet_mt(ip, 23, 150)
print('%s正在升级请稍后...'% ip)
# 检测到Router#就输入命令
tn.read_until('#'.encode())
for command in commands:
tn.write(command.encode('ascii')+b'\r\n')
tn.read_all()
# 关闭连接
tn.close()
except:
print('%s telnet连接断开,升级成功...' % ip)
self.get_ver(ip, 2500)
# telnet连接终端
def telnet_mt(self, ip, port, timeout):
username = 'admin'
password = 'admin'
tn = telnetlib.Telnet(ip, port, timeout)
tn.set_debuglevel(5)
tn.read_until('Username:'.encode())
# 输入用户名
tn.write(username.encode('ascii') + b'\r\n')
tn.read_until('Password:'.encode())
# 输入密码
tn.write(password.encode('ascii') + b'\r\n')
return tn
# 获取终端版本信息
def get_ver(self, ip, port):
try:
tn = self.telnet_mt(ip, port, 10)
tn.read_until('>'.encode())
tn.write('mtver'.encode() + b'\r\n')
tn.read_until('>'.encode())
tn.close()
print('%s 获取版本信息成功!' % ip)
except:
print('%s获取版本信息成功失败' % ip)
# 显示未连接成功终端
def show_failed(self):
count = 0
print('所有终端升级完成')
print('未连接成功终端如下:')
for j in self.ips:
print(j)
count += 1
print('共%d台' % count)
if __name__ == '__main__':
start = Update()
start.the_start()