-
Notifications
You must be signed in to change notification settings - Fork 0
/
wlcsshshell.py.old
executable file
·354 lines (326 loc) · 10.8 KB
/
wlcsshshell.py.old
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
'''
SSH connection to WLC controller
'''
import paramiko
import time
import re
if __name__ == '__main__':
ip = '192.168.4.100'
# ip = "10.130.0.1"
username = 'admin'
password = 'C1sc0123'
# username = 'cisco'
# password = 'C1sco123'
credentials = {
'ip': ip,
'username': username,
'password': password,
}
ftp_settings = {
'filename' : 'AIR-CT5520-K9-8-2-141-0.aes',
'username' : 'ftpuser',
'password' : 'C!sco123',
'serverip' : '192.168.4.100',
'path' : '/Users/ftpuser/tftpboot'
}
class WlcSshShell(object):
'''
Class to handle WLC ssh connections
'''
DEBUG = False
def check_prompt(self, output):
'''
Check whether the prompt is ready
:param output:
:return:
'''
pattern = '>'
try:
if output[-1] == pattern:
return True
else:
return False
except IndexError:
# When the output is empty
return False
def __init__(self, ip, username, password, port=22):
'''
Opens SSH connection and logins to the WLC
'''
self.ip = ip
self.username = username
self.password = password
self.session_log = ''
self.port = port
connect_param = {
'username': username,
'password': password,
'port' : port,
'look_for_keys': False,
'allow_agent': False,
}
# create ssh connection
self.remote_conn = paramiko.SSHClient()
# add remote keys (maybe not super safe, but will do for my use)
self.remote_conn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
if self.DEBUG:
print('Opening SSH connection')
# Opens ssh connection
self.remote_conn.connect(self.ip, **connect_param)
# invoke a shell session
self.shell_session = self.remote_conn.invoke_shell()
time.sleep(1)
if self.DEBUG:
print('SSH connection opened successfully')
self.login()
if self.DEBUG:
print('Login successful')
def is_alive(self):
'''
Check whether ssh connection is alive
:return:
'''
try:
transport = self.remote_conn.get_transport()
transport.send_ignore()
except EOFError as e:
return False
return True
# connection is closed
def read(self, verbose=False):
'''
Read output from the ssh session
:return: output as UTF string
'''
output = b''
while self.shell_session.recv_ready():
buffer = self.shell_session.recv(10000)
if self.DEBUG:
print('RECEIVED: {}'.format(buffer.decode()))
elif verbose:
print(buffer.decode())
# time.sleep(0.1)
output += buffer
return output.decode()
def write(self, command):
'''
Write a command to the WLC ssh session
It clears the input and add a trailing newline if not present
An empty string input will send a newline
'''
if self.DEBUG:
print('SENT: {}'.format(command))
# clean the input:
# appends a newline if is not there and removes empty strings
try:
if command[-1] != '\n':
command = '{}\n'.format(command)
except IndexError:
"If empty string sends a newline"
command = '\n'
# converts to bytes and send
command_b = command.encode()
self.shell_session.send(command_b)
# time.sleep(0.1)
def send_command(self, command, verbose=False, pattern=None, delay=None):
'''
send a command and displays the output
:param command
:return: ssh session output
'''
command_log = ''
# If the channel is not ready, read it
if not (self.shell_session.send_ready()):
command_log = self.read(verbose)
self.write(command)
if delay is not None:
time.sleep(delay)
while True:
output = self.read(verbose)
command_log += output
# Checks if prompt has returned
if self.expected_prompt(output):
break
# Take care of 'Press Enter to continue'
elif self.expected_prompt(output, 'Press Enter to continue.*'):
# if Press Enter to continue found, send newline
self.write('\n')
elif pattern is not None:
try:
if self.expected_prompt(output.splitlines()[-1], pattern):
# print('Warning, I''m sending a command with a warning')
self.write('y')
except:
pass
self.session_log += command_log
return command_log
def send_commands(self, command_list, verbose=False, pattern=None, delay = None):
'''
Send a sequence of commands, one by one
:param command_list:
:param verbose:
:return:
'''
command_log = []
if type(command_list) is list:
for command in command_list:
output = self.send_command(command, verbose, pattern, delay)
command_log.append((command,output))
else:
output = self.send_command(command_list, verbose, pattern, delay)
command_log = (command_list, output)
return command_log
def expected_prompt(self, output, expected='>'):
'''
Check whether the output is matching the expected prompt
:param expected: expected pattern, default if '>'
:param output: output to parse
:return:
E.g. Use this to check wether I am getting "User:" in the prompt
'''
pattern = r'{}\s*$'.format(expected)
# look for pattern trailing the the output
# breaks the output in lines and only look in the latest
try:
if re.search(pattern, output.splitlines()[-1]) is not None:
return True
else:
return False
except IndexError:
return False
def login(self):
'''
logs in the WLC
'''
# output = ''
while True:
# read output
buffer = self.read()
# output += buffer
# If the prompt is "User:" will send the username
if self.expected_prompt(buffer, 'User:'):
self.write(self.username)
# If the prompt is "Password:" will send the pw
if self.expected_prompt(buffer, 'Password:'):
self.write(self.password)
# once I have a prompt, the login is successful
# (PROMPT)>
elif self.expected_prompt(buffer):
break
# disable paging
self.disable_paging()
# if self.DEBUG:
# print(output)
def disable_paging(self):
'''
Disable paging on the WLC
:return:
'''
self.send_command('config paging disable')
def load_image(self, username, password, filename, serverip, path, transfer_proto='ftp'):
'''
Download an image on the controller
:param transfer_proto:
:param username:
:param password:
:return:
'''
if transfer_proto not in ('ftp', 'tftp', 'sftp'):
raise AttributeError('transfer_proto has to be ftp, tftp, sftp')
commands = '''\
transfer download filename {filename}
transfer download mode {transfer_proto}
transfer download username {username}
transfer download password {password}
transfer download serverip {serverip}
transfer download path {path}
transfer download datatype code
transfer download start\
'''.format(filename=filename,
transfer_proto=transfer_proto,
username=username,
password=password,
serverip=serverip,
path=path).splitlines()
if TROUBLESHOOT:
commands = ['transfer download start']
output = self.send_commands(commands, pattern=r'Are you sure you want to start\? \(y/N\)')
def save_config(self, username, password, filename, serverip, path, transfer_proto='ftp'):
'''
Download an image on the controller
:param transfer_proto:
:param username:
:param password:
:return:
'''
if transfer_proto not in ('ftp', 'tftp', 'sftp'):
raise AttributeError('transfer_proto has to be ftp, tftp, sftp')
commands = '''\
transfer upload filename {filename}
transfer upload mode {transfer_proto}
transfer upload username {username}
transfer upload password {password}
transfer upload serverip {serverip}
transfer upload path {path}
transfer upload datatype config
transfer upload start\
'''.format(filename=filename,
transfer_proto=transfer_proto,
username=username,
password=password,
serverip=serverip,
path=path).splitlines()
output = self.send_commands(commands, pattern=r'Are you sure you want to start\? \(y/N\)')
def show_run(self, file=None):
'''
Returns running-config and saves it to a file(optional)
:return:
'''
output = self.send_command('show running-config')
if file is not None:
with open(file, 'w') as f:
f.write(output)
return output
def show_run_commands(self, file=None):
'''
Returns running-config and saves it to a file(optional)
:return:
'''
output = self.send_command('show run-config commands')
if file is not None:
with open(file, 'w') as f:
f.write(output)
return output
def show_run_startup(self, file=None):
'''
Returns running-config and saves it to a file(optional)
:return:
'''
output = self.send_command('show run-config startup-commands')
if file is not None:
with open(file, 'w') as f:
f.write(output)
return output
def close(self):
'''
Terminate the shell session
:return:
'''
self.remote_conn.close()
if __name__ == '__main__':
WlcSshShell.DEBUG = True
wlc_session = WlcSshShell(**credentials)
# output = wlc_session.show_run_commands('/Users/rmartini/Desktop/wlc01-c')
# wlc_session.load_image(**ftp_settings)
#output = wlc_session.show_run_startup('/Users/rmartini/Desktop/startup_config')
# output = wlc_session.show_run_commands('/Users/rmartini/Desktop/wlc01-c')
# output = wlc_session.send_command('show sysinfo')
# output = wlc_session.send_command('show sysinfo')
# output = wlc_session.show_run('show-run.txt')
#
# if DEBUG:
# print ('End of show run')
# else:
# exit(0)
#
#