forked from day0ng/w-sw-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathw-sw-ssh.py
814 lines (671 loc) · 24.9 KB
/
w-sw-ssh.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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
#!/usr/bin/env python
"""
Emulates SSH login and executes cli commands as user interactive to network
devices such as switches, routers etc.
Copyright (c) Dayong Wang, [email protected]
Distributable under the terms of the GNU General Public License
version 2. Provided with no warranties of any sort.
Revision history
~~~~~~~~~~~~~~~~
2015/11/07
creates by Dayong Wang:
- Just remove some unnecessary information.
2016/10/11
bug fix by Dayong Wang:
- Exit while password is wrong and ask for input again.
Last commit info:
~~~~~~~~~~~~~~~~~
$LastChangedDate: $
$Rev: $
$Author: $
"""
import getopt
import getpass
import os
import pexpect
import re
import subprocess
import sys
import threading
import time
def help_and_exit():
print('''
Usage: %s <options>
--uid <uid> SSH username
--pwd <pwd> SSH password
-p Get password from user input
--host <ip[:port],...> ip[:port] list of remote ssh server, default port is 22
--host_file <file_name> File of ip[:port] list
--cmd <cmd1;cmd2;...> Command list to execute on remote ssh server
--cmd_prefix <file_prefix> Prefix of command list files. For example:
test.cmd.cisco
test.cmd.cisco_nexus
test.cmd.h3c
test.cmd.huawei
'test' is the prefix (--cmd_prefix).
--cmd_interval <seconds> Time to wait after a command being executed, default is 0.5s.
And some devices would get error if execute command too fast.
--save Save config on device automatically after user cmd being executed.
--log_dir <path> Log command output to /<path>/<ip_addr> instead of stdout.
Example:
/var/log/test/$(date "+%%Y")/$(date "+%%Y%%m%%d")/
--thread <num> The maximum threads could be spread each time, default is 1000.
--timeout <seconds> Time to wait for command executing, default is 10 seconds.
Try to set higher value in case of seeing 'pexpect timed out' error.
--l2_sw Check the layer-2 switch only infomation, such as uplink, gateway etc.
Caution:
--host has higher priority than --host_file
--cmd has higher priority than --cmd_prefix
Example:
w-sw-ssh.py --uid npc -p --host 192.168.161.10 --cmd "disp users"
w-sw-ssh.py --uid npc -p --host_file ~/ip.test --cmd_prefix ~/cmd.test
''' % (os.path.basename(__file__)))
sys.exit()
def w_time(time_format = '%Y-%m-%d %H:%M:%S'):
return time.strftime(time_format, time.localtime(time.time()))
def sys_cmd(str_cmd):
sp = subprocess.Popen(str_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
str_out = sp.stdout.read()
str_err = sp.stderr.read()
sp.wait()
return [str_out, str_err]
def w_threading(func_name, func_args, max_thread):
# multi threading
if func_name == None or func_name == '':
print('w_threading() error: func_name is empty.\n')
return False
if func_args == None or not isinstance(func_args, list):
print('w_threading() error: func_args is wrong.\n')
return False
if not isinstance(max_thread, int) or max_thread == None or max_thread == '':
max_thread = 1000
# create thread pool
thread_pool = list()
for i in range(0, len(func_args)):
th = threading.Thread(target=func_name, args=func_args[i])
thread_pool.append(th)
# execute threads for max_thread number of threads each time
thread_count = len(thread_pool)
if thread_count > max_thread:
i_begin = 0
i_end = 0
round_num = thread_count / max_thread
if thread_count % max_thread > 0:
round_num += 1
# max_thread: How many threads (test) could be executed at one time
for j in range(0, round_num):
i_begin = j * max_thread
if j == round_num - 1: # the last round
i_end = thread_count
else:
i_end = i_begin + max_thread
# start threads
for i in range(i_begin, i_end):
thread_pool[i].start()
# terminate threads
for i in range(i_begin, i_end):
thread_pool[i].join()
# === thread_count <= max_thread ===
else:
# start threads
for i in range(0, thread_count):
thread_pool[i].start()
# terminate threads
for i in range(0, thread_count):
thread_pool[i].join()
# ========== Run threads - End ==========
#___ End of w_threading() ____
def uf_login_expect(ssh, timeout, f_out):
#
#_____ yes/no | password _____
#
# [root@SERVER bin]# ssh [email protected]
# The authenticity of host '172.22.131.63 (172.22.131.63)' can't be established.
# RSA key fingerprint is 9c:9c:e2:41:7d:83:76:80:d5:fa:97:38:da:fe:4d:23.
# Are you sure you want to continue connecting (yes/no)? yes
# Warning: Permanently added '172.22.131.63' (RSA) to the list of known hosts.
# [email protected]'s password:
# Password incorrect.
#
# Login failed, check your uid or pwd.Permission denied, please try again.
# [email protected]'s password:
#
#_____ known_hosts _____
#
# [root@SERVER bin]# ssh [email protected]
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
# Someone could be eavesdropping on you right now (man-in-the-middle attack)!
# It is also possible that the RSA host key has just been changed.
# The fingerprint for the RSA key sent by the remote host is
# 9c:9c:e2:41:7d:83:76:80:d5:fa:97:38:da:fe:4d:23.
# Please contact your system administrator.
# Add correct host key in /root/.ssh/known_hosts to get rid of this message.
# Offending key in /root/.ssh/known_hosts:2330
# RSA host key for 172.22.131.63 has changed and you have requested strict checking.
# Host key verification failed.
# [root@SERVER bin]#
#
cmd_out = ''
try:
idx = ssh.expect(['(P|p)assword: $', '\(yes/no\)\?', 'Host key verification failed'], timeout=timeout)
cmd_out = '%s%s' % (ssh.before, ssh.after)
if f_out == None:
print(cmd_out)
else:
f_out.write(cmd_out)
return [idx, cmd_out]
except:
return [-1, cmd_out]
def uf_login_fix_known_hosts(cmd_out):
tmp_out = cmd_out.split("\n")
for tmp_row in tmp_out:
# Offending key in /root/.ssh/known_hosts:2330
if re.search('known_hosts:[0-9]+', tmp_row, re.IGNORECASE) == None:
continue
tmp_row = re.sub('^.* /', '/', tmp_row).strip()
tmp_hosts, tmp_line = tmp_row.split(':')
tmp_cmd = "sed -i '%sd' %s" % (tmp_line, tmp_hosts)
return sys_cmd(tmp_cmd)
return ''
def uf_login_send_yes(ssh, sleep_time):
ssh.sendline('yes')
time.sleep(sleep_time)
return True
def uf_login_send_pwd(ssh, sleep_time, pwd):
ssh.sendline(pwd)
time.sleep(sleep_time)
return True
def uf_ssh_login(ssh, timeout, output_file, f_out, ip, port, uid, pwd, sleep_time):
#
# Depends on:
#
# uf_login_expect()
# uf_login_fix_known_hosts()
# uf_login_send_yes()
# uf_login_send_pwd()
#
idx, cmd_out = uf_login_expect(ssh, timeout, f_out) # login expect
# login error
if idx == -1:
print('[%s] %s:%s Error: uid <%s> login failed (1)' % (w_time(), ip, port, uid))
return False
# wrong known_hosts
if idx == 2:
uf_login_fix_known_hosts(cmd_out)
print('[%s] %s:%s Error: Host key was fixed and try again.' % (w_time(), ip, port))
return False
# ask for yes/no
if idx == 1:
uf_login_send_yes(ssh, sleep_time)
idx, cmd_out = uf_login_expect(ssh, timeout, f_out) # login expect
# ask for password
if idx == 0:
uf_login_send_pwd(ssh, sleep_time, pwd)
return True
def uf_expect_prompt(ssh, timeout, f_out):
#
# Prompt:
#
# ^MBJ_XX_311-F-02_N7718-1#
# <BJ_XX_305-A-15_CE5810>
# <BJ-XXX-101-1109/1111-LVS-S5500>
# ^@<BJ_XX_311_F-12-13_LVS_S5560>
# BJ-XXX-2-2960S-2016#
# [~BJ_XX_320-I-10_CE5810]
#
# Characters:
#
# < > a-z A-Z 0-9 ~ @ * / _ - [] ()
#
# Q: Why put a . next to \\n)?
# A: Some devices output a prompt likes ^@<BJ_XX_311_F-12-13_LVS_S5560>
#
prompt = "(\\r|\\n).?[<>a-zA-Z0-9~@\*/_\-\[\]\(\)]+(>|%|#|\\$|\]) *$"
cmd_out = ''
try:
idx = ssh.expect([prompt, pexpect.TIMEOUT], timeout=timeout)
cmd_out = '%s%s' % (ssh.before, ssh.after)
if f_out == None:
print(cmd_out)
else:
f_out.write(cmd_out)
return [idx, cmd_out]
except:
return [-1, cmd_out]
def uf_expect_sendline(ssh, timeout, f_out, sleep_time, content):
ssh.sendline('')
ssh.sendline(content)
time.sleep(sleep_time)
return uf_expect_prompt(ssh, timeout, f_out)
def uf_get_vendor_model(ssh, timeout, f_out, sleep_time):
vendor = ''
model = ''
cmd_out = ''
#___ Get vendor ___
reg_vendor_search = ''
reg_vendor_sub = ''
# 1st, for h3c or huawei devices
tmp_cmd = 'display version | in (Huawei|H3C).*(Software|uptime)'
idx, cmd_out = uf_expect_sendline(ssh, timeout, f_out, sleep_time, tmp_cmd)
if idx == 1:
print("[%s] %s:%s Error: pexpect timed out." % (w_time(), ip, port))
return [vendor, model]
if re.search('% Invalid|Unrecognized command', cmd_out) == None:
if cmd_out.find('H3C ') >= 0:
vendor = '%s%s' % (vendor, 'h3c')
reg_vendor_search = '^h3c.*uptime'
reg_vendor_sub = ' *uptime.*$'
if cmd_out.find('Huawei ') >= 0:
vendor = '%s%s' % (vendor, 'huawei')
reg_vendor_search = '^huawei.*uptime'
reg_vendor_sub = ' *uptime.*$'
# 2nd, for cisco devices
else:
tmp_cmd = 'show version | in Cisco.*Software|cisco.*(Chassis|processor)'
idx, cmd_out = uf_expect_sendline(ssh, timeout, f_out, sleep_time, tmp_cmd)
if idx == 1:
print("[%s] %s:%s Error: pexpect timed out." % (w_time(), ip, port))
return [vendor, model]
if cmd_out.find('Cisco Nexus ') >= 0:
vendor = 'cisco_nexus'
reg_vendor_search = '^cisco.*chassis'
reg_vendor_sub = ' *(\(|chassis).*$'
elif cmd_out.find('Cisco ') >= 0:
vendor = 'cisco'
reg_vendor_search = '^cisco.*processor'
reg_vendor_sub = ' *(\(|chassis).*$'
#___ Get model ___
tmp_row = ''
tmp_out = cmd_out.split('\n')
for tmp_row in tmp_out:
tmp_row = tmp_row.strip()
if re.search(reg_vendor_search, tmp_row, re.IGNORECASE) == None:
continue
else:
model = re.sub(reg_vendor_sub, '', tmp_row, re.IGNORECASE)
model = re.sub('^(cisco nexus|cisco|h3c|huawei) *', '', model, re.IGNORECASE)
break
# Return
return [vendor, model]
def uf_set_nomore(ssh, timeout, f_out, sleep_time, vendor):
cmd_nomore = ''
if vendor == 'cisco':
cmd_nomore = 'terminal length 0'
if vendor == 'cisco_nexus':
cmd_nomore = 'terminal length 0'
if vendor == 'h3c':
cmd_nomore = 'screen-length disable'
if vendor == 'huawei':
cmd_nomore = 'screen-length 0 temp'
return uf_expect_sendline(ssh, timeout, f_out, sleep_time, cmd_nomore)
def uf_save(ssh, timeout, f_out, sleep_time, vendor):
cmd_save = ''
if vendor == 'cisco':
cmd_save = 'end\rcopy run start\r'
if vendor == 'cisco_nexus':
cmd_save = 'copy run start'
if vendor == 'h3c':
cmd_save = 'save force'
if vendor == 'huawei':
cmd_save = 'return\rsave\r\y\r'
return uf_expect_sendline(ssh, timeout, f_out, sleep_time, cmd_save)
def uf_logout(ssh, timeout, f_out, sleep_time, vendor):
cmd_logout = ''
if vendor == 'cisco':
cmd_logout = 'end\rexit'
if vendor == 'cisco_nexus':
cmd_logout = 'end\rexit'
if vendor == 'h3c':
cmd_logout = 'quit\rquit\r'
if vendor == 'huawei':
cmd_logout = 'quit\rquit\r'
return uf_expect_sendline(ssh, timeout, f_out, sleep_time, cmd_logout)
def uf_get_l2_uplink(ssh, timeout, f_out, sleep_time, vendor):
l2_uplink = ''
gw_ip = ''
gw_mac = ''
cmd_get_gw_ip = ''
cmd_get_gw_mac = ''
cmd_get_gw_uplink = ''
reg_get_gw_ip_search = '\s?[1-9]\d{0,2}(\.\d{1,3}){3}\s?' # match ip but not 0.0.0.0
reg_get_gw_mac_search = '\s?([\da-f]{4}[\.-]){2}[\da-f]{4}\s?'
reg_get_gw_uplink_sub = ''
# Init cmd
if vendor == 'cisco':
cmd_get_gw_ip = 'show ip default-gateway'
cmd_get_gw_mac = 'show ip arp _IP_'
cmd_get_gw_uplink = 'show mac address-table address _MAC_'
if vendor == 'cisco_nexus':
cmd_get_gw_ip = 'show ip route 0.0.0.0/0'
cmd_get_gw_mac = 'show ip arp _IP_'
cmd_get_gw_uplink = 'show mac address-table address _MAC_'
if vendor == 'h3c':
cmd_get_gw_ip = 'display ip routing-table 0.0.0.0 0'
cmd_get_gw_mac = 'disp arp _IP_'
cmd_get_gw_uplink = 'display mac-address _MAC_'
if vendor == 'huawei':
cmd_get_gw_ip = 'display ip routing-table 0.0.0.0 0'
cmd_get_gw_mac = 'disp arp dynamic | include _IP_'
cmd_get_gw_uplink = 'display mac-address _MAC_'
# Get gateway IP
idx, cmd_out = uf_expect_sendline(ssh, timeout, f_out, sleep_time, cmd_get_gw_ip)
if idx == 1:
print("[%s] %s:%s Error: pexpect timed out." % (w_time(), ip, port))
return l2_uplink
if idx == -1:
return l2_uplink
tmp_row = ''
tmp_out = cmd_out.split('\n')
for tmp_row in tmp_out:
tmp_row = tmp_row.strip()
tmp_re = re.search(reg_get_gw_ip_search, tmp_row, re.IGNORECASE)
if tmp_re == None:
continue
else:
gw_ip = tmp_re.group(0).strip()
break
if gw_ip == '':
return l2_uplink
# Get gateway MAC
cmd_get_gw_mac = re.sub('_IP_', gw_ip, cmd_get_gw_mac)
idx, cmd_out = uf_expect_sendline(ssh, timeout, f_out, sleep_time, cmd_get_gw_mac)
if idx == 1:
print("[%s] %s:%s Error: pexpect timed out." % (w_time(), ip, port))
return l2_uplink
if idx == -1:
return l2_uplink
tmp_row = ''
tmp_out = cmd_out.split('\n')
for tmp_row in tmp_out:
tmp_row = tmp_row.strip()
tmp_re = re.search(reg_get_gw_mac_search, tmp_row, re.IGNORECASE)
if tmp_re == None:
continue
else:
gw_mac = tmp_re.group(0).strip()
break
if gw_mac == '':
return l2_uplink
# Get gateway uplink
cmd_get_gw_uplink = re.sub('_MAC_', gw_mac, cmd_get_gw_uplink)
idx, cmd_out = uf_expect_sendline(ssh, timeout, f_out, sleep_time, cmd_get_gw_uplink)
if idx == 1:
print("[%s] %s:%s Error: pexpect timed out." % (w_time(), ip, port))
return l2_uplink
if idx == -1:
return l2_uplink
tmp_row = ''
tmp_out = cmd_out.split('\n')
for tmp_row in tmp_out:
tmp_row = tmp_row.strip()
if re.search(cmd_get_gw_uplink, tmp_row) != None:
continue
tmp_re = re.search(reg_get_gw_mac_search, tmp_row, re.IGNORECASE)
if tmp_re == None:
continue
else:
l2_uplink = re.sub('^.*%s' % (gw_mac), '', tmp_row).strip()
break
if l2_uplink != '':
l2_uplink_list = l2_uplink.split()
if vendor == 'h3c':
l2_uplink = l2_uplink_list[2]
elif vendor == 'huawei':
l2_uplink = l2_uplink_list[1]
elif vendor == 'cisco_nexus':
l2_uplink = l2_uplink_list[4]
elif vendor == 'cisco':
l2_uplink = l2_uplink_list[1]
else:
l2_uplink = ''
return l2_uplink
def w_main(ip, port, uid, pwd, cmd, cmd_prefix, cmd_interval, log_dir, flt_timeout, save, l2_sw):
#_________ start of arguments init _________
# arg: ip
if not isinstance(ip, str) or ip.strip() == '':
print('[%s] Error: incorrect IP address <%s>' % (w_time(), ip))
return False
# arg: port
if not isinstance(port, str) or port.strip() == '':
port = '22'
# arg: uid
if not isinstance(uid, str) or uid.strip() == '':
print('[%s] %s:%s Error: incorrect UID' % (w_time(), ip, port))
return False
# arg: pwd
if not isinstance(pwd, str) or pwd.strip() == '':
print('[%s] %s:%s Error: incorrect PWD' % (w_time(), ip, port))
return False
# arg: cmd, cmd_prefix
cmd_list = list()
if not isinstance(cmd, str) or cmd == None or cmd.strip() == '':
if not isinstance(cmd_prefix, str) or cmd_prefix == None or cmd_prefix.strip() == '':
print('[%s] %s:%s Warning: neither --cmd nor --cmd_prefix was specified.\n' % (w_time(), ip, port))
else:
cmd_list = None
# Then go to place where vendor was alreay identified.
else:
cmd_list = cmd.split(';')
# arg: cmd_interval, default 0.5
if isinstance(cmd_interval, float):
sleep_time = cmd_interval
else:
sleep_time = 0.5
if sleep_time <= 0:
sleep_time = 0.5
# arg: log_dir
if not isinstance(log_dir, str) or log_dir == None or log_dir.strip() == '':
output_file = ''
else:
output_file = '%s/%s' % (log_dir, ip)
output_path = os.path.dirname(output_file)
if not os.path.exists(output_path):
try:
sys_cmd('mkdir -p %s' % (output_path))
except:
print('[%s] %s:%s Error: mkdir %s failed!' % (w_time(), ip, port, output_path))
return False
if os.path.exists(output_path):
try:
f_out = open(output_file, 'w')
except:
print('[%s] %s:%s Error: file %s is failed to open.' % (w_time(), ip, port, output_file))
return False
else:
output_file = ''
if output_file == '':
f_out = None
# arg: timeout
if isinstance(flt_timeout, float):
timeout = flt_timeout
else:
timeout = 10
# arg: save
if not isinstance(save, str) or save.strip() == '':
save = 'no'
# arg: l2_sw
if not isinstance(l2_sw, str) or l2_sw.strip() == '':
l2_sw = 'no'
#_________ end of arguments init _________
# Login - ssh
try:
print('[%s] ssh -p %s -l %s %s' % (w_time(), port, uid, ip))
ssh = pexpect.spawn('ssh -p %s -l %s %s' % (port, uid, ip))
time.sleep(sleep_time)
except:
print('[%s] %s:%s Error: ssh failed' % (w_time(), ip, port))
return False
if not uf_ssh_login(ssh, timeout, output_file, f_out, ip, port, uid, pwd, sleep_time):
return False
idx, cmd_out = uf_expect_prompt(ssh, timeout, f_out)
if idx != 0:
return False
# Get vendor and model
vendor, model = uf_get_vendor_model(ssh, timeout, f_out, sleep_time)
if vendor == '':
print("[%s] %s:%s Error: can not get device vendor." % (w_time(), ip, port))
return False
if model == '':
print("[%s] %s:%s Error: can not get device type." % (w_time(), ip, port))
return False
# Set no-more
idx, cmd_out = uf_set_nomore(ssh, timeout, f_out, sleep_time, vendor)
if idx == 1:
print("[%s] %s:%s Error: pexpect timed out." % (w_time(), ip, port))
return False
# Get l2-uplink
l2_uplink = ''
if l2_sw == 'yes':
l2_uplink = uf_get_l2_uplink(ssh, timeout, f_out, sleep_time, vendor)
# if cmd_prefix was prefered.
if cmd_list == None:
cmd_prefix = '%s.cmd.%s' % (cmd_prefix, vendor)
if not os.path.exists(cmd_prefix):
print('[%s] %s:%s Error: %s does not exist.\n' % (w_time(), ip, port, cmd_prefix))
return False
f_cmd = open(cmd_prefix)
cmd_list = f_cmd.readlines()
f_cmd.close()
# Execute the command
cmd_idx = 0
cmd_all = ''
for i in range(0, len(cmd_list)):
cmd_idx = i + 2
cmd_line = cmd_list[i].strip()
cmd_all = '%s\n%s) %s' % (cmd_all, str(i+1).rjust(5), cmd_line)
time.sleep(sleep_time)
try:
idx, cmd_out = uf_expect_sendline(ssh, timeout, f_out, sleep_time, cmd_line)
if idx == 1:
print("[%s] %s:%s Error: pexpect timed out." % (w_time(), ip, port))
return False
except:
print('\n[%s] %s:%s Error: command %s is failed to be executed.' % (w_time(), ip, port, cmd_list[i].strip()))
# Save config
if save == 'yes':
if timeout < 10:
timeout = 10
idx, cmd_out = uf_save(ssh, timeout, f_out, sleep_time, vendor)
cmd_all = '%s\n%s) %s' % (cmd_all, str(cmd_idx).rjust(5), '[Save Config]')
if idx == 1:
print("[%s] %s:%s Error: save config timed out." % (w_time(), ip, port))
return False
if idx == -1:
print("[%s] %s:%s Error: save config failed." % (w_time(), ip, port))
# Logout
uf_logout(ssh, timeout, f_out, sleep_time, vendor)
ssh.close()
the_end = '''
=======================================
Device: %s
Vendor: %s
Model: %s
L2_Uplink: %s
Commands: %s
\r\n''' % (ip, vendor, model, l2_uplink, cmd_all)
if f_out == None:
print(the_end)
else:
f_out.write(the_end)
f_out.close()
return True
#___ End of w_main() ___
if __name__ == '__main__':
host = ''
uid = ''
pwd = ''
cmd = ''
host_file = ''
cmd_prefix = ''
cmd_interval = 0.5
log_dir = ''
thread = 1000
timeout = 10
save = 'no'
l2_sw = 'no'
try:
opts, args = getopt.getopt(sys.argv[1:], "hp", ['uid=','pwd=','host=','host_file=','cmd=','cmd_prefix=','cmd_interval=','log_dir=','thread=','timeout=','save','l2_sw'])
except:
print("Wrong options!")
print("Try '-h' to get more information.")
sys.exit()
if len(opts) == 0:
help_and_exit()
for op, value in opts:
if op == '-h':
help_and_exit()
elif op == '--uid':
uid = value
elif op == '--pwd':
pwd = value
elif op == '-p':
pwd = getpass.getpass()
elif op == '--host':
host = value
elif op == '--host_file':
host_file = value
elif op == '--cmd':
cmd = value
elif op == '--cmd_prefix':
cmd_prefix = value
elif op == '--cmd_interval':
try:
cmd_interval = float(value)
except ValueError:
print('Wrong option: --cmd_interval only accepts a float value.')
print("Try '-h' to get more information.")
sys.exit(1)
elif op == '--log_dir':
log_dir = value
elif op == '--thread':
if value.isalnum():
thread = int(value)
elif op == '--timeout':
try:
timeout = float(value)
except ValueError:
print('Wrong option: --timeout only accepts a float value.')
print("Try '-h' to get more information.")
sys.exit(1)
elif op == '--save':
save = 'yes'
elif op == '--l2_sw':
l2_sw = 'yes'
else:
help_and_exit()
#__________ multi-thread __________
# func_name
func_name = w_main
# func_args
func_args = list()
print('')
if host != '':
host_list = host.split(',')
elif host_file != '':
if not os.path.exists(host_file):
print('%s does not exist, please specify host with --host or --host_file.\n' % (host_file))
help_and_exit()
f_host = open(host_file)
host_list = f_host.readlines()
f_host.close()
else:
print('Please specify host with --host or --host_file.\n')
help_and_exit()
host_len = len(host_list)
for i in range(0, host_len):
if host_list[i].find(':') >= 0:
ip, port = host_list[i].split(':')
ip = ip.strip()
port = port.strip()
else:
ip = host_list[i].strip()
port = ''
func_args.append([ip, port, uid, pwd, cmd, cmd_prefix, cmd_interval, log_dir, timeout, save, l2_sw])
# Start multi-threading
w_threading(func_name, func_args, thread)
# exit
print('')
sys.exit()