-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathciscomasscmd.py
61 lines (55 loc) · 1.77 KB
/
ciscomasscmd.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
#!flask/bin/python
import napalm
import sys
import os
import re
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import as_completed
DEBUG = 1
def go(user, password, devices, commands):
resp = ''
if DEBUG: print("Found devices:", devices)
if DEBUG: print("Found commands:", commands)
with ThreadPoolExecutor(max_workers = 4) as executor:
future_to_res = {executor.submit(exec_commands, user, password, device, commands): device for device in devices}
for future in as_completed(future_to_res):
try:
data = future.result()
except Exception as exc:
resp += "Exception:" + str(exc)
else:
resp += data
return resp
def exec_commands(user, password, dev_name, commands):
#return if nothing to do
if len(dev_name) == 0 or len(commands) == 0:
return
driver = napalm.get_network_driver('ios')
output = ""
#througout all devices and execute all command per device
try:
output += "--== " + dev_name + " ==--\n"
try:
dev = driver(dev_name, user, password)
dev.open()
try:
for cmd in commands:
if DEBUG: print("->", cmd);
output += "->" + cmd + "\n"
out = dev.device.send_command_expect(cmd, "#")
if DEBUG: print("<-", out)
output += "<-" + out + "\n"
except:
output += "Exception, cannot execute cmd:" + cmd + "\n"
print("Exception, cannot execute cmd:", cmd)
dev.close()
except:
output += "Exception, cannot connect to:" + dev_name + "\n"
print("Exception, cannot connect to:", dev_name)
except:
output += "Something goes wrong" + "\n"
print("Something goes wrong")
#print(output)
return output