-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlldp.py
131 lines (106 loc) · 3.75 KB
/
lldp.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
import collections
from collections import OrderedDict
from ipaddress import *
from cisco import Cisco
class LLDP():
"""
class for get the lldp information
"""
# List of devices we've already checked
_name_ip = {}
def get_name_by_ip(self, ip_range):
"""
return a dictonary with the ip of the network that are Cisco devices
"""
hosts = list(ip_network(ip_range, False).hosts())
# get the name of one host
for x in hosts:
device_host = str(x)
print device_host
try:
d = Cisco(host=device_host, community='CAMPUS')
self._name_ip[d.name] = device_host
except:
print "The device isn't Cisco"
return self._name_ip
# list of the nodes of the network
_nodes = []
# list of the links of the network
_links = []
# variables for the checking of the algorithm
_cheked = []
_insert = []
# property for get the value of attribute nodes and links
@property
def nodes(self):
"""
returns the attribute that is the result of the method get_nodes
"""
return self._nodes
@property
def links(self):
"""
returns the attribute that is the result of the method get_links
"""
return self._links
def get_nodes_links(self, device, network, cost_hop=1.000):
"""
find the nodes and links of a network
"""
# add a node in the list _nodes
try:
# prevent the dublication of nodes
if device.name not in self._insert:
node = {"id": device.host,
"label": device.name}
self._nodes.append(node)
self._insert.append(device.name)
except:
print "device not recognized"
return {"id": "", "label": ""}
# find the neighbours of the device
try:
neighbours = device.get_neighbours_names()
except:
return
# if it is an end point return
if not neighbours:
return
# Loop prevention
for i in range(0, len(neighbours)):
x = neighbours[i].items()[0][1]
if x and (x not in self._cheked):
try:
# find the ip of the device
# from the dictonary of the network
my_host = network[x]
# create the new Cisco device
d = Cisco(host=my_host, community='CAMPUS')
# add a link to the list
link = OrderedDict([("source", device.host),
("target", d.host),
("cost", cost_hop)])
self._links.append(link)
# Recurse!
self._cheked.append(x)
add = self.get_nodes_links(d, network, cost_hop)
if add is not None:
self._nodes.append(add)
except KeyError:
print "The device is not in the network!"
self._cheked.append(x)
return
# get the value in NetJSON
def network_to_netJSON(self, device, network, cost=1.000,):
# get the network and find the info
network_devices = self.get_name_by_ip(network)
self.get_nodes_links(device, network_devices, cost)
net = collections.OrderedDict(
[('type', 'NetworkGraph'),
('protocol', 'l2'),
('version', '0.1'),
('router_id', device.host),
('label', 'Urbino network'),
('nodes', self._nodes),
('links', self._links)])
return net