-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify.py
111 lines (94 loc) · 3.69 KB
/
verify.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
import json, os, time, re
from pprint import pprint, pformat
from threading import Thread
from lib.telnet import TELNET
from lib.ssh import SSH
from feature import *
def show_vlan_show_trunk(dev):
session = TELNET(host=dev['host'], username=dev['username'], password=dev['password'], port=dev['port'])
connection = session.connect()
print(connection, pformat(dev))
content_vlan = ""
connection.write(b"en\n")
connection.write(b"show vlan\n")
while True:
n, match, previous_text = connection.expect([b"--More--"], 2)
# print(n, match, previous_text)
content_vlan += previous_text.decode()
if n in [0, 1]:
connection.write(b" ")
else:
content_vlan = re.sub(r"\s--More--\s\x08\x08\x08\x08\x08\x08\x08\x08\x08 \x08\x08\x08\x08\x08\x08\x08\x08\x08", '', content_vlan)
break
content_trunk = ""
connection.write(b"show int trunk\n")
while True:
n, match, previous_text = connection.expect([b"--More--"], 2)
# print(n, match, previous_text)
content_trunk += previous_text.decode()
if n in [0, 1]:
connection.write(b" ")
else:
content_trunk = re.sub(r"\s--More--\s\x08\x08\x08\x08\x08\x08\x08\x08\x08 \x08\x08\x08\x08\x08\x08\x08\x08\x08", '', content_trunk)
break
content_etherchannel = ""
connection.write(b"show etherchannel detail\n")
while True:
n, match, previous_text = connection.expect([b"--More--"], 2)
# print(n, match, previous_text)
content_etherchannel += previous_text.decode()
if n in [0, 1]:
connection.write(b" ")
else:
content_etherchannel = re.sub(r"\s--More--\s\x08\x08\x08\x08\x08\x08\x08\x08\x08 \x08\x08\x08\x08\x08\x08\x08\x08\x08", '', content_etherchannel)
break
session.disconnect()
pprint(content_vlan)
pprint(content_trunk)
pprint(content_etherchannel)
create_file(dev['name'] + '_vlan', './BCMSN_Campus/solution/', content_vlan)
create_file(dev['name'] + '_trunk', './BCMSN_Campus/solution/', content_trunk)
create_file(dev['name'] + '_etherchannel', './BCMSN_Campus/solution/', content_etherchannel)
def show_ip_route(dev):
session = SSH(device_type='cisco_ios', ip=dev['ip'], username=dev['username'], password=dev['password'],
source_address=dev['host'])
connection = session.connect()
connection.enable()
routes = connection.send_command('show ip route', use_textfsm=True)
session.disconnect()
print('ip={}, routes={}'.format(dev['host'], pformat(routes)))
backup_json(routes, './BCMSN_Campus/solution/{}_routes.json'.format(dev['name']))
def main():
# Load data from inventory
file_path = './BCMSN_Campus/inventory.json'
hosts_all = restore_json(file_path)
'''Switches'''
data = [dev for dev in hosts_all if dev['type'] in ['switch', 'switch_L3']]
threads = []
for dev in data:
t = Thread(target=show_vlan_show_trunk, args= (dev,))
t.start()
threads.append(t)
for t in threads:
t.join()
'''Switch Layer 3 & Routers'''
username = input('username: ')
password = input('password: ')
devices = []
for dev in hosts_all:
if dev['type'] in ['router', 'switch_L3']:
dev['username'] = username
dev['password'] = password
dev['port'] = 22
devices.append(dev)
# devices = devices[:1]
pprint(devices)
threads = []
for dev in devices:
t = Thread(target=show_ip_route, args= (dev,))
t.start()
threads.append(t)
for t in threads:
t.join()
if __name__ == '__main__':
main()