-
Notifications
You must be signed in to change notification settings - Fork 6
/
Color_Asyncio_Netdev.py
74 lines (47 loc) · 1.5 KB
/
Color_Asyncio_Netdev.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
#!/usr/bin/python3
import asyncio
import netdev
import os, time, sys ,socket, json
from termcolor import colored, cprint
USER = 'cisco'
PASSWORD = 'Cisco123'
start = time.time()
# Define username and password to login to all routers with
Switches_dic=[]
Switches_IP=[]
with open('IPaddress_Switches.txt') as f:
for line in f:
line = line.strip()
try:
socket.inet_aton(line)
Switches_IP.append(line)
IP = { 'username' : USER,'password' : PASSWORD,'device_type': 'cisco_ios','host': line}
Switches_dic.append(IP)
except socket.error:
print ("Invalid IP address " + line)
cprint ("This is Switches IPs: \n","red")
print (Switches_IP)
print("\n")
os.chdir("output/")
async def task(param):
async with netdev.create(**param) as ios:
# Get the device Hostname
hostname = await ios.send_command("show run | inc hostname")
hostname = hostname[9:]
hostname.split(" ")
print (hostname, end='') ,print(colored(" Done","yellow"))
output = await ios.send_command("show run")
f = open((hostname), "w")
print((output), file=f) # python 3.x
async def run():
tasks = [task(switch) for switch in Switches_dic]
await asyncio.wait(tasks)
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
end = time.time()
total = int(end - start)
t1= colored('\n Elapsd Time: ' , 'white')
t2=(str(total) + " Sec\n")
t3=colored(t2,'green')
print(t1+t3)
sys.exit(1)