forked from Buffer94/NetHound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkElements.py
382 lines (310 loc) · 14.4 KB
/
NetworkElements.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
from builtins import print
class DHCPServer:
def __init__(self, ip_address, mac_address, subnet):
self.ip_address = ip_address
self.mac_address = mac_address
self.subnet = subnet
self.no_response_count = 0
def print_info(self):
msg = 'Ip Address: %s MAC address: %s Subnet %s' % (self.ip_address, self.mac_address, self.subnet)
print(msg)
return msg
def set_ip_address(self, ip_address):
self.ip_address = ip_address
def restore_response_count(self):
self.no_response_count = 0
def increase_response_count(self):
self.no_response_count += 1
def equals(self, n_mac_address):
return True if self.mac_address == n_mac_address else False
class DNSServer:
def __init__(self, ip_address, mac_address):
self.ip_address = ip_address
self.mac_address = mac_address
self.no_response_count = 0
def print_info(self):
msg = 'Ip Address: %s MAC address: %s' % (self.ip_address, self.mac_address)
print(msg)
return msg
def set_ip_address(self, ip_address):
self.ip_address = ip_address
def restore_response_count(self):
self.no_response_count = 0
def increase_response_count(self):
self.no_response_count += 1
def equals(self, n_mac_address):
return True if self.mac_address == n_mac_address else False
class ARPTable:
def __init__(self):
self.ip_arp_table = dict()
self.mac_arp_table = dict()
def add_couple(self, ip, mac):
if ip in self.ip_arp_table:
if not (mac in self.ip_arp_table[ip]):
self.ip_arp_table[ip].append(mac)
if len(self.ip_arp_table[ip]) > 1:
print("Conflict Found, duplicate IP address: %s with this mac: %s" % (ip, self.ip_arp_table[ip]))
else:
self.ip_arp_table[ip] = list()
self.ip_arp_table[ip].append(mac)
if len(self.ip_arp_table[ip]) > 1:
print("Conflict Found, duplicate IP address: %s with this mac: %s" % (ip, self.ip_arp_table[ip]))
if mac in self.mac_arp_table:
if not (ip in self.mac_arp_table[mac]):
self.mac_arp_table[mac].append(ip)
if len(self.mac_arp_table[mac]) > 1:
print("Conflict Found, duplicate mac address: %s with this IPs: %s" % (mac, self.mac_arp_table[mac]))
else:
self.mac_arp_table[mac] = list()
self.mac_arp_table[mac].append(ip)
if len(self.mac_arp_table[mac]) > 1:
print("Conflict Found, duplicate mac address: %s with this IPs: %s" % (mac, self.mac_arp_table[mac]))
def print_ip_arp_table(self):
for ip in self.ip_arp_table:
print("IP %s - MAC: %s" % (ip, str(self.ip_arp_table[ip])[1:-1]))
def print_mac_arp_table(self):
for mac in self.mac_arp_table:
print("MAC %s - IP: %s" % (mac, str(self.mac_arp_table[mac])[1:-1]))
class SpanningTreeInstance:
def __init__(self, vlan_id):
self.ports = list()
self.vlan_id = vlan_id
self.priority = 60000
self.bridge_id = None
self.root_bridge_id = None
self.root_bridge = False
self.tc_counter = 0
def get_blocked_port(self):
out = list()
for port in self.ports:
if port.pvlan_status[self.vlan_id] == "Blocked":
out.append(port)
return out
def add_port(self, port):
if port not in self.ports:
self.ports.append(port)
def remove_port(self, port):
if port in self.ports:
self.ports.remove(port)
def increase_tc_counter(self):
self.tc_counter += 1
def update_stp_info(self, priority, bridge_id, root_bridge_id):
self.priority = int(priority) + int(self.vlan_id)
self.bridge_id = bridge_id
self.root_bridge_id = root_bridge_id
self.check_root_bridge()
def check_root_bridge(self):
self.root_bridge = True if self.bridge_id == self.root_bridge_id else False
def there_is_root_port(self):
for port in self.ports:
if port.pvlan_status[self.vlan_id] == "Root":
return True
return False
def print_stp_status(self):
msg = "Spanning Tree on Vlan: %s\n" % self.vlan_id
print("Spanning Tree on Vlan: %s" % self.vlan_id)
print(" Root Bridge: %s - Bridge: %s - Priority: %s" % (self.root_bridge_id, self.bridge_id, self.priority))
msg += " Root Bridge: %s - Bridge: %s - Priority: %s \n" % (self.root_bridge_id, self.bridge_id, self.priority)
if self.root_bridge:
print(" This switch is the Root Bridge")
msg += " This switch is the Root Bridge\n"
print(" Recents Topology Change: %s" % self.tc_counter)
msg += " Recents Topology Change: %s\n" % self.tc_counter
for port in self.ports:
if port.negotiation:
print("\tPort: %s(!) - Address: %s, Status: %s - #Rec_CNG: %s" % (port.name, port.MAC,
port.pvlan_status[self.vlan_id],
port.pvlan_status_change_counter[
self.vlan_id]))
msg += "\tPort: %s(!) - Address: %s, Status: %s - #Rec_CNG: %s\n" % (port.name, port.MAC,
port.pvlan_status[self.vlan_id],
port.pvlan_status_change_counter[
self.vlan_id])
else:
print("\tPort: %s - Address: %s, Status: %s - #Rec_CNG: %s" % (port.name, port.MAC,
port.pvlan_status[self.vlan_id],
port.pvlan_status_change_counter[self.vlan_id]))
msg += "\tPort: %s - Address: %s, Status: %s - #Rec_CNG: %s\n" % (port.name, port.MAC,
port.pvlan_status[self.vlan_id],
port.pvlan_status_change_counter[self.vlan_id])
print("(!) - Port allow trunk negotiations!")
msg += "(!) - Port allow trunk negotiations!\n"
return msg
class Switch:
def __init__(self, n, ip, pwd, en_pwd, conn_interface):
self.name = n
self.bridge_id = None
self.ports = list()
self.ip = ip
self.password = pwd
self.en_password = en_pwd
self.connected_interface = conn_interface
self.spanning_tree_instances = dict()
def get_interfaces(self):
interfaces = list()
for port in self.ports:
interfaces.append(port.name)
return interfaces
def set_stp_priority(self, vlan, priority):
self.spanning_tree_instances[vlan].priority = int(priority) + int(vlan)
def set_stp_root_id(self, vlan, root_id):
self.spanning_tree_instances[vlan].root_bridge_id = root_id
self.spanning_tree_instances[vlan].check_root_bridge()
def add_ports(self, port):
if port not in self.ports:
self.ports.append(port)
def set_designated_port(self, port_address, vlan_id, override=False, priority=None, b_id=None, initialization=False):
for port in self.ports:
if port.MAC == port_address:
self.add_port_to_spanning_tree(vlan_id, port, priority, b_id)
port.set_port_as_designated(vlan_id, override, initialization)
def set_blocked_port(self, port_address, vlan_id, override=False, priority=None, b_id=None, initialization=False):
for port in self.ports:
if port.MAC == port_address:
self.add_port_to_spanning_tree(vlan_id, port, priority, b_id)
port.set_port_as_blocked(vlan_id, override, initialization)
def set_root_port(self, port_address, vlan_id, override=False, priority=None, b_id=None, initialization=False):
for port in self.ports:
if port.MAC == port_address:
self.add_port_to_spanning_tree(vlan_id, port, priority, b_id)
port.set_port_as_root(vlan_id, override, initialization)
def print_spanning_tree(self):
msg = ''
for vlan_id in self.spanning_tree_instances:
self.spanning_tree_instances[vlan_id].ports.sort(key=self.take_MAC)
msg += '%s \n' % self.spanning_tree_instances[vlan_id].print_stp_status()
return msg
def print_trunk_ports(self):
out_msg = 'Trunk Ports:\n'
print("Trunk Ports:")
there_is_trunks = False
for port in self.ports:
if port.trunk:
there_is_trunks = True
msg = "Port %s - vlans: %s" % (port.name, port.print_vlans())
out_msg += '%s \n' % msg
print(msg)
if not there_is_trunks:
print("In this switch there are not Trunk Ports!\n")
out_msg += 'In this switch there are not Trunk Ports!\n'
else:
print('')
return out_msg
@staticmethod
def take_MAC(port):
return port.MAC
def get_port(self, port_mac):
for port in self.ports:
if port.MAC == port_mac:
return port
def get_port_by_name(self, port_name):
for port in self.ports:
if port.name == port_name:
return port
def contains(self, port_address):
for port in self.ports:
if port.MAC == port_address:
return True
if port_address == self.bridge_id:
return True
return False
def increase_tc_counter(self, vlan):
self.spanning_tree_instances[vlan].increase_tc_counter()
def increase_port_tc_counter(self, vlan, port_mac):
self.get_port(port_mac).increase_status_change_counter(vlan)
def add_port_to_spanning_tree(self, vlan_id, port, priority=None, r_id=None):
if vlan_id in self.spanning_tree_instances:
self.spanning_tree_instances[vlan_id].add_port(port)
else:
self.spanning_tree_instances[vlan_id] = SpanningTreeInstance(vlan_id)
self.spanning_tree_instances[vlan_id].add_port(port)
if r_id is not None and priority is not None:
self.spanning_tree_instances[vlan_id].update_stp_info(priority, self.bridge_id, r_id)
def remove_port_from_stp(self, vlan_id, port):
self.spanning_tree_instances[vlan_id].remove_port(port)
if len(self.spanning_tree_instances[vlan_id].ports) == 0:
del self.spanning_tree_instances[vlan_id]
def there_is_root_port(self, vlan_id):
return self.spanning_tree_instances[vlan_id].there_is_root_port()
def all_root_port_found(self):
for vlan_id in self.get_vlans():
if not self.spanning_tree_instances[vlan_id].root_bridge:
found = self.spanning_tree_instances[vlan_id].there_is_root_port()
if not found:
return False
return True
def get_trunk_port(self):
out = list()
for port in self.ports:
if port.trunk:
out.append(port)
return out
def get_blocked_port(self):
out = list()
for port in self.ports:
if len(port.pvlan_status) == 0 or port.trunk:
out.append(port)
return out
def get_blocked_port_per_vlan(self, vlan_id):
out = list()
for port in self.ports:
if port.pvlan_status[vlan_id] == 'Blocked':
out.append(port)
return out
def get_vlans(self):
return self.spanning_tree_instances.keys()
class Port:
def __init__(self, n, m):
self.name = n
self.MAC = m
self.pvlan_status = dict()
self.pvlan_status_change_counter = dict()
self.trunk = False
self.negotiation = False
self.negotiation_rcvd = False
self.no_nego_count = 0
def set_port_as_designated(self, vlan_id=1, override=False, initialization=False):
if len(self.pvlan_status) > 0 and vlan_id not in self.pvlan_status and not self.trunk:
self.trunk = True
if override or vlan_id not in self.pvlan_status:
self.pvlan_status[vlan_id] = "Designated"
if initialization:
self.pvlan_status_change_counter[vlan_id] = 0
def set_port_as_root(self, vlan_id=1, override=False, initialization=False):
if len(self.pvlan_status) > 0 and vlan_id not in self.pvlan_status and not self.trunk:
self.trunk = True
if override or vlan_id not in self.pvlan_status:
self.pvlan_status[vlan_id] = "Root"
if initialization:
self.pvlan_status_change_counter[vlan_id] = 0
def set_port_as_blocked(self, vlan_id=1, override=False, initialization=False):
if len(self.pvlan_status) > 0 and vlan_id not in self.pvlan_status and not self.trunk:
self.trunk = True
if override or vlan_id not in self.pvlan_status:
self.pvlan_status[vlan_id] = "Blocked"
if initialization:
self.pvlan_status_change_counter[vlan_id] = 0
def increase_status_change_counter(self, vlan):
if vlan in self.pvlan_status_change_counter:
self.pvlan_status_change_counter[vlan] += 1
else:
self.pvlan_status_change_counter[vlan] = 0
def get_vlan(self):
return list(self.pvlan_status.keys())
def print_vlans(self):
out = list()
for key in self.get_vlan():
out.append(key)
return str(out)[1:-1]
def contain_vlan(self, vlan_id):
if vlan_id in self.pvlan_status:
return True
return False
def remove_vlan(self, vlan_id, log=None):
if vlan_id in self.pvlan_status:
del self.pvlan_status[vlan_id]
if len(self.pvlan_status) < 2 and self.trunk:
self.trunk = False
print("Port %s is no longer TRUNK!" % self.name)
if log is not None:
log.write("Port %s is no longer TRUNK!" % self.name)