-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathneighbor_info.py
156 lines (121 loc) · 5.04 KB
/
neighbor_info.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import re
import json
import datetime
from pprint import pprint
from csv import DictReader
from getpass import getpass
from switch_classes import NxSwitch, IosSwitch, EosSwitch
def main():
"""
This function loads an inventory CSV and uses the appropriate
function to parse the data into a dictionary with keys:
'neighbor_interface', 'local_interface', and 'neighbor.' Each
device in the inventory file will have a list with these
dictionaries.
:return: This will pretty print the neighbors, and also save
a file named 'neighbors_(date).json' for historical data.
"""
user = input('What is your username: ')
pw = getpass('What is your password: ')
inventory = DictReader(open('inventory.csv'))
inv_dict = {}
for device in inventory:
if device['os'] == 'nxos':
inv_dict[device['hostname']] = nxos_cdp_filter(device['ip'], user, pw)
elif device['os'] == 'ios':
inv_dict[device['hostname']] = ios_cdp_filter(device['ip'], user, pw)
elif device['os'] == 'eos':
inv_dict[device['hostname']] = eos_lldp_filter(device['hostname'])
pprint(inv_dict)
datestamp = datetime.date.today()
fout = open('neighbors_{}.json'.format(datestamp), 'w')
fout.write(json.dumps(inv_dict))
fout.close()
def nxos_cdp_filter(device, user, pw=None):
"""
This function is used to retrieve and filter cdp
data on Nexus switches using the NxSwitch class.
:param device: The switch to retrieve cdp neighbors from.
:param user: The user account to login to the switch.
:param pw: The password for the user account; if blank
getpass will be used to retrieve it more securely.
:return: A list of dictionaries with the filtered cdp neighbor
information. The keys are: 'neighbor_interface', 'local_interface',
and 'neighbor.'
"""
nxos_switch = NxSwitch(device, user, pw)
cdp_request = nxos_switch.nx_cdp()
if cdp_request.ok:
cdp_data = nxos_switch.nx_cdp().json()
cdp_filtered = cdp_data['result']['body']['TABLE_cdp_neighbor_brief_info']['ROW_cdp_neighbor_brief_info']
all_neighbors = []
for neighbor in cdp_filtered:
neighbor_dict = {
"neighbor_interface": neighbor['port_id'],
"local_interface": neighbor['intf_id'],
"neighbor": neighbor['device_id']
}
all_neighbors.append(neighbor_dict)
return all_neighbors
else:
print('The request failed! status_code: {}\nreason: {}\ncontent: {}'.format(
cdp_request.status_code, cdp_request.reason, cdp_request.content
))
def ios_cdp_filter(device, user, pw=None):
"""
This function is used to retrieve and filter cdp
data on IOS devices using the IosSwitch class.
The regex works whether the cdp neighbor takes one
or more lines, and accounts for varying capabilities.
:param device: The switch to retrieve cdp neighbors from.
:param user: The user account to login to the switch.
:param pw: The password for the user account; if blank
getpass will be used to retrieve it more securely.
:return: A list of dictionaries with the filtered cdp neighbor
information. The keys are: 'neighbor_interface', 'local_interface',
and 'neighbor.'
"""
try:
ios_switch = IosSwitch(device, user, pw)
cdp_data = ios_switch.ios_cdp()
rel_cdp_info = cdp_data.split('Port ID\n')[1]
regex = '(\S+)\s+(\S+ \S+)\s+(\S+)\s+((\S \S \S \S \S)|(\S \S \S \S)|(\S \S \S)|(\S \S)|(\S))\s+(\S+)\s+(.*)'
cdp_neighbors = re.findall(regex, rel_cdp_info)
all_neighbors = []
for neighbor in cdp_neighbors:
neighbor_dict = {
"neighbor_interface": neighbor[10],
"local_interface": neighbor[1],
"neighbor": neighbor[0]
}
all_neighbors.append(neighbor_dict)
return all_neighbors
except:
print("Couldn't login into {}".format(device))
def eos_lldp_filter(device):
"""
This function is used to retrieve and filter lldp
data on Arista switches using the EosSwitch class.
:param device: The connection value for the switch in the
configuratoin file for pyeapi.
:return: A list of dictionaries with the filtered cdp neighbor
information. The keys are: 'neighbor_interface', 'local_interface',
and 'neighbor.'
"""
eos_switch = EosSwitch(device)
try:
eos_switch_conn = eos_switch.eos_connect()
lldp_data = eos_switch.eos_lldp_neighbors(eos_switch_conn)[0]
all_neighbors = []
for neighbor in lldp_data['result']['lldpNeighbors']:
neighbor_dict = {
"neighbor_interface": neighbor['neighborPort'],
"local_interface": neighbor['port'],
"neighbor": neighbor['neighborDevice']
}
all_neighbors.append(neighbor_dict)
return all_neighbors
except:
print("Couldn't login into: {}".format(device))
if __name__ == '__main__':
main()