-
Notifications
You must be signed in to change notification settings - Fork 6
/
MultiThreading.py
100 lines (73 loc) · 2.6 KB
/
MultiThreading.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
#!/usr/bin/python3
import threading, os, time, sys ,socket
from multiprocessing import Queue
from getpass import getpass
from netmiko import ConnectHandler
from netmiko.ssh_exception import NetMikoTimeoutException
from paramiko.ssh_exception import SSHException
from netmiko.ssh_exception import AuthenticationException
start = time.time()
# Define username and password to login to all routers with
USER = 'cisco'
PASSWORD = 'Cisco123'
# Define Switches IPs
Switches=[]
with open('IPaddress_Switches.txt') as f:
for line in f:
line = line.strip()
try:
socket.inet_aton(line)
Switches.append(line)
except socket.error:
print ("Invalid IP address " + line)
print ("This is Switches IPs: \n")
print (Switches)
print("\n")
os.chdir("output/")
def ssh_session(switch, output_q):
# Place what you want each thread to do here, for example connect to SSH, run a command, get output
output_dict = {}
switch_d = {'device_type': 'cisco_ios', 'ip': switch, 'username': USER, 'password': PASSWORD}
# SSH Iteration
try:
ssh_session = ConnectHandler(**switch_d)
except (AuthenticationException):
print("Wrong Authentication >>> "+ (switch_d['ip']) + "\n")
pass
except (NetMikoTimeoutException):
print("Timeout >>> "+ (switch_d['ip']) + "\n")
pass
except (EOFError):
print("EOF Error >>> "+ (switch_d['ip']) + "\n")
pass
except (SSHException):
print("Error SSH Exception >>> "+ (switch_d['ip']) + "\n")
pass
except Exception as unknown_error:
print("Unkown Error >>> "+ (switch_d['ip']) + "\n")
pass
else:
# Get the device Hostname
hostname = ssh_session.send_command('show run | inc host')
hostname = hostname[9:]
hostname.split(" ")
print (hostname)
# Put the Output into a Dictionary to use it later with threading Queue
output = ssh_session.send_command("show run ")
output_dict[hostname] = output
output_q.put(output_dict)
# Save the output into a file with Device Hostname
f = open((hostname), "w")
print((output), file=f) # python 3.x
# print >>f, (output) #python 2.x
if __name__ == "__main__":
output_q = Queue()
# Start thread for each switch in Switches list
for switch in Switches:
my_thread = threading.Thread(target=ssh_session, args=(switch, output_q))
my_thread.start()
my_thread.join()
end = time.time()
total = int(end - start)
print("\n Elapsd Time: " + str(total) + " Sec\n")
sys.exit(1)