-
Notifications
You must be signed in to change notification settings - Fork 6
/
Asyncio_Netdev.py
67 lines (43 loc) · 1.36 KB
/
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
#!/usr/bin/python3
import asyncio
import netdev
import os, time, sys ,socket, json
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)
print ("This is Switches IPs: \n")
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)
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)
print("\n Elapsd Time: " + str(total) + " Sec\n")
sys.exit(1)