-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand-runner.py
128 lines (111 loc) · 5.97 KB
/
command-runner.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
#Note that before running this script, you need to create a folder called "output" in the same directory that you intend to run this from.
#Note2 please edit the "devices" file to match your devices.
#Note3 please edit the "vars.py" file and adjust your credentials etc
from netmiko import ConnectHandler
import io
import time
import getpass
import vars
import os
from pyfiglet import Figlet
company = vars.company
pt = vars.port
device = vars.devicetype
localtime = time.localtime()
formattime = time.strftime("%d-%m-%Y %H:%M:%S", localtime)
datetime = time.strftime("%d-%m-%Y", localtime)
f = Figlet(font="standard", width=90)
print(f.renderText(company + " Command Runner"))
print("Please enter the credentials to perform the tasks required:\r\n")
print("Username: ", end="")
usern = str(input())
password = getpass.getpass()
secret = getpass.getpass(prompt='Secret: ')
print("\r\nSelect mode, s = show/run, c = configuration or v = verification commands:")
mode = input()
devices = dict()
if mode == "s":
print("Enter command to run on all devices:")
command = input()
if mode == "c":
print("Enter configuration commands with care. When finished press Ctrl-D (Ctrl-Z on Windows) once to break then please wait.")
cmdset = []
while True:
try:
cmdline = input()
except EOFError:
break
cmdset.append(cmdline)
if mode == "v":
print("Enter configuration to verify existence on all devices:")
command = input()
if not os.path.exists("output"):
os.mkdir("output")
if not os.path.exists("output/command-runner"):
os.mkdir("output/command-runner")
file = open("devices","r")
for line in file:
devices.update({line.split(",")[0]:line.split(",")[1]})
for dev_name, dev_address in devices.items():
try:
sw = {
'device_type': device,
'ip': dev_address.strip(),
'username': usern,
'password': password,
'secret': secret,
'port' : pt,
'verbose': False
}
net_connect = ConnectHandler(**sw)
net_connect.enable()
if mode == "c":
output = net_connect.send_command('term len 0')
output = net_connect.send_config_set(cmdset)
print("\r\n##################\r\nConfiguration sent to " + dev_name + ":\r\n" + output + "\r\n##################\r\n")
if mode == "s":
output = net_connect.send_command('term len 0')
output = net_connect.send_command(command)
fi = open(os.path.join("output/command-runner/cli output " + command.replace(" ","_") + " " + datetime + ".txt"), "a")
fi.write("\r\n##################\r\n" + dev_name + "\r\n" + command + "\r\n\r\n" + output + "\r\n##################\r\n\r\n")
fi.close()
print("\r\n##################\r\n" + dev_name + "\r\n" + command + "\r\n\r\n" + output + "\r\n##################\r\n\r\n")
if mode == "v":
output = net_connect.send_command('term len 0')
output = net_connect.send_command('show run')
print("\r\n##################\r\nChecking " + dev_name + " running configuration contains: " + command + "\r\n")
if command in output:
print(" - Exists!\r\n##################\r\n")
fi = open(os.path.join("output/command-runner/cli verify " + command.replace(" ","_") + " " + datetime + ".txt"), "a")
fi.write("\r\n##################\r\n" + dev_name + "\r\n" + command + " - Exists!\r\n##################\r\n")
fi.close()
else:
print(" - Does not exist!\r\n##################\r\n")
fi = open(os.path.join("output/command-runner/cli verify " + command.replace(" ","_") + " " + datetime + ".txt"), "a")
fi.write("\r\n##################\r\n" + dev_name + "\r\n" + command + " - Does not exist!\r\n##################\r\n")
fi.close()
net_connect.disconnect()
except:
if mode == "c":
fi = open(os.path.join("output/command-runner/FAILED CONFIG DEVICES " + datetime + ".txt"), "a")
fi.write("\r\n##################\r\nFAILED at " + formattime + " on device: " + dev_name + "\r\nConfiguration attempted:\r\n" + output + "\r\nCheck ssh access to: " + dev_address)
fi.close()
print("\r\n##################\r\n" + "!!!! " + dev_name + " is unreachable !!!!\r\nCheck SSH access to: " + dev_address + "##################\r\n")
if mode == "s":
fi = open(os.path.join("output/command-runner/FAILED DEVICES " + command.replace(" ","_") + " " + datetime + ".txt"), "a")
fi.write("\r\n##################\r\nFAILED at " + formattime + " on device: " + dev_name + "\r\nCheck ssh access to: " + dev_address)
fi.close()
print("\r\n##################\r\n" + "!!!! " + dev_name + " is unreachable !!!!\r\nCheck SSH access to: " + dev_address + "##################\r\n")
if mode == "v":
fi = open(os.path.join("output/command-runner/FAILED VERIFY DEVICES " + command.replace(" ","_") + " " + datetime + ".txt"), "a")
fi.write("\r\n##################\r\nFAILED at " + formattime + " on device: " + dev_name + "\r\nCheck ssh access to: " + dev_address)
fi.close()
print("\r\n##################\r\n" + "!!!! " + dev_name + " is unreachable !!!!\r\nCheck SSH access to: " + dev_address + "##################\r\n")
continue
if(mode == "s"):
print("\r\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\nShow/run command sent to all devices in list. Outputs have been saved to the output folder. Press enter to quit.\r\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\n")
if(mode == "c"):
print("\r\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\nConfiguration commands have been sent to all devices in list. Please verify if successful. Press enter to quit.\r\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\n")
if(mode == "v"):
print("\r\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\nVerification of command sent to all devices in list is completed. Please review the results. Press enter to quit.\r\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\n")
input()