forked from f13rce/SpanningTreePracticer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stp.py
694 lines (587 loc) · 23.5 KB
/
stp.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
#!/usr/bin/env python3
#######################################################################
#######################################################################
#######################################################################
# _____ _ _______ #
# / ____| (_) |__ __| #
# | (___ _ __ __ _ _ __ _ __ _ _ __ __ _ | |_ __ ___ ___ #
# \___ \| '_ \ / _` | '_ \| '_ \| | '_ \ / _` | | | '__/ _ \/ _ \ #
# ____) | |_) | (_| | | | | | | | | | | | (_| | | | | | __/ __/ #
# |_____/| .__/ \__,_|_|_|_|_| |_|_|_| |_|\__, | |_|_| \___|\___| #
# | __ \| | | | (_) __/ | #
# | |__) |_|_ __ _ ___| |_ _ ___ ___ |___/ #
# | ___/ '__/ _` |/ __| __| |/ __/ _ \ #
# | | | | | (_| | (__| |_| | (_| __/ #
# |_| |_| \__,_|\___|\__|_|\___\___| #
# #
#######################################################################
# Made by Ivar Slotboom :) #
#######################################################################
#######################################################################
# Imports
from termcolor import colored # colored outputs
from random import randrange # random bridge numbers
from argparse import ArgumentParser # argument parser
import sys # stdout without endl
import os # clear screen
import time # sleep
import math # floor
import random # range
network = [] # network[X][Y] = Edge,Network,(or)Bridge
highlightColor = "yellow"
highlightW = -1
highlightH = -1
rootW = -1
rootH = -1
# Classes
class Edge:
classType = "Edge"
representationHor = "-XY-"
representationVer = "|XY|"
representation = representationHor
type = 0 # 0 = Unassigned; 1 = Root Port; 2 = Designated Port; 3 = Blocked Port;
typeColors = ["white", "blue", "green", "red"]
isHor = True
color = typeColors[0]
answer = "??"
port = -1
def UseHorizontalRepresentation(self):
self.representation = self.representationHor
self.isHor = True
def UseVerticalRepresentation(self):
self.representation = self.representationVer
self.isHor = False
def SetPortNumber(self, aVal):
self.port = aVal
portStr = list(str(aVal))
reprStr = list(self.representation)
reprStr[1] = portStr[0]
reprStr[2] = portStr[1]
self.representation = "".join(reprStr)
def SetEdgeType(self, aName):
# Naming in representation
reprStr = list(self.representation)
reprStr[1] = aName[0]
reprStr[2] = aName[1]
self.representation = "".join(reprStr)
# Coloring
if aName == "RP":
self.color = self.typeColors[1]
elif aName == "DP":
self.color = self.typeColors[2]
elif aName == "BP":
self.color = self.typeColors[3]
else:
self.color = self.typeColors[0]
def SetAnswer(self, aAnswer):
self.answer = aAnswer
def IsBlocked(self):
return self.answer == "BP"
class Network:
classType = "Network"
networkRepresentation = "{XX}"
representation = "{XX}"
name = ""
color = "cyan"
def SetName(self, aName):
self.name = aName
self.representation = self.networkRepresentation.replace("XX", aName)
class Bridge:
classType = "Bridge"
bridgeRepresentation = "[XX]"
representation = "[XX]"
color = "magenta"
value = 0
def SetValue(self, aValue):
self.value = aValue
self.representation = self.bridgeRepresentation.replace("XX", "{}".format(aValue))
class Empty:
classType = "Empty"
representation = " "
color = "grey"
# Funcs
def parse_args(args=None):
parser = ArgumentParser("This script will help you practise with spanning tree protocol topologies")
parser.add_argument('--disable-banner', action='store_true', help='Disable script banner')
parser.add_argument('-s', '--skip-abbreviations', action='store_true', help='Do not ask abbreviation questions')
size_args = parser.add_argument_group(
'Network size',
'Specify the network size parameters, these can be between 3 and 11 bridges '
'and it has to be an odd number of bridges'
)
size_args.add_argument(
'-w',
'--width',
type=int,
choices=list(filter(lambda x: (x % 2 != 0), range(3, 12))),
default=7,
help='The width of the topology in bridges (default: 7)'
)
size_args.add_argument(
'-H',
'--height',
type=int,
choices=list(filter(lambda x: (x % 2 != 0), range(3, 12))),
default=7,
help='The height of the topology in bridges (default: 7)'
)
return parser.parse_args(args)
def GenerateField(networkWidth, networkHeight):
global network
# Initialize network array
for w in range(networkWidth):
network.append([])
for h in range(networkHeight):
network[w].append(None)
randValues = []
portNumbers = []
networkNameCount = 0
for w in range(networkWidth):
for h in range(networkHeight):
# Bridge
if (w + h) % 4 == 0 and h % 2 == 0:
# Pick a random number as hop value - make sure it's unique
number = 0
while True:
number = 10 + randrange(89)
if not number in randValues:
randValues.append(number)
break
# Make bridge
network[w][h] = Bridge()
network[w][h].SetValue(number)
# Networks
elif w % 2 == 0 and h % 2 == 0:
char = chr(ord('A') + networkNameCount)
char = "{}{}".format(char, char) # AA, BB, CC, etc
networkNameCount += 1
network[w][h] = Network()
network[w][h].SetName(char)
# Edges
elif w % 2 == 0 or h % 2 == 0:
network[w][h] = Edge()
if w % 2 != 0:
network[w][h].UseVerticalRepresentation()
else:
network[w][h].UseHorizontalRepresentation()
# Pick a random number as hop value - make sure it's unique
number = 0
while True:
number = 10 + randrange(89)
if number not in portNumbers:
portNumbers.append(number)
break
network[w][h].SetPortNumber(number)
# Empty
else:
network[w][h] = Empty()
def DrawField(networkWidth, networkHeight):
global highlightW
global highlightH
# For every entry, print it
for w in range(networkWidth):
for h in range(networkHeight):
if highlightW == w and highlightH == h:
sys.stdout.write(colored(network[w][h].representation, highlightColor))
else:
sys.stdout.write(colored(network[w][h].representation, network[w][h].color))
sys.stdout.write("\n")
def GetRootID(networkWidth, networkHeight):
# Find root ID based on the lowest bridge value
lowestID = 99999
for w in range(networkWidth):
for h in range(networkHeight):
if network[w][h].classType == "Bridge":
if network[w][h].value < lowestID:
lowestID = network[w][h].value
# Debug
# print("Root ID: {}".format(lowestID))
return lowestID
rootPaths = []
def SolveEdgeLabeling(networkWidth, networkHeight):
global rootPaths
global rootW
global rootH
# Get root ID
rootID = int(GetRootID(networkWidth, networkHeight))
# Find root X/Y
bridges = []
for w in range(networkWidth):
for h in range(networkHeight):
if network[w][h].classType == "Bridge":
if network[w][h].value == rootID:
rootW = w
rootH = h
else:
bridges.append((w, h))
# Set default values for edges
for w in range(networkWidth):
for h in range(networkHeight):
if network[w][h].classType == "Edge":
network[w][h].SetAnswer("BP")
# Find every path possible per bridge
for bridge in bridges:
rootPaths = []
# print("Finding paths to root ({}) from bridge {}".format(repr((rootW, rootH)), repr((bridge[0], bridge[1]))))
path = []
GetPaths(0, path, bridge[0], bridge[1], networkWidth, networkHeight)
paths = rootPaths
# Find shortest paths based on hop count
shortestPaths = []
shortestLen = 99999
for path in rootPaths:
if len(path) == 0:
continue
if len(path) < shortestLen:
shortestPaths = []
shortestLen = len(path)
shortestPaths.insert(0, path)
# print("Shortest path length: {} | Total paths: {}".format(shortestLen, len(shortestPaths)))
# Tie breaker: find shortest path based on hop values from bridges
minScore = 99999
shortestCountPaths = []
for shortPath in shortestPaths:
count = 0
for entry in shortPath:
if network[entry[0]][entry[1]].classType == "Bridge":
count += network[entry[0]][entry[1]].value
if count < minScore:
minScore = count
shortestCountPaths = []
shortestCountPaths.append(shortPath)
elif count == minScore:
shortestCountPaths.append(shortPath)
# print("Shortest route: {} | Path: {}".format(minScore, repr(shortestPath)))
# Tie breaker: Lowest port number
shortestPath = None
if len(shortestCountPaths) > 1:
minPort = 999999
for path in shortestCountPaths:
if network[path[1][0]][path[1][1]].port < minPort:
minPort = network[path[1][0]][path[1][1]].port
shortestPath = path
else:
shortestPath = shortestCountPaths[0]
# Solve it
isFromBridge = False
# for entry in shortestPath:
for i in range(len(shortestPath)):
entry = shortestPath[i]
if network[entry[0]][entry[1]].classType == "Bridge":
isFromBridge = True
elif network[entry[0]][entry[1]].classType == "Network":
isFromBridge = False
elif network[entry[0]][entry[1]].classType == "Edge":
if isFromBridge:
network[entry[0]][entry[1]].SetAnswer("RP")
else:
network[entry[0]][entry[1]].SetAnswer("DP")
# Apply port numbers
# Find duplicate RP entries, but pick one where the others become a BP
for w in range(networkWidth):
for h in range(networkHeight):
rootPorts = []
if network[w][h].classType == "Bridge":
if w > 0:
if network[w - 1][h].classType == "Edge":
if network[w - 1][h].answer == "RP":
rootPorts.append((w - 1, h))
if w < networkWidth - 1:
if network[w + 1][h].classType == "Edge":
if network[w + 1][h].answer == "RP":
rootPorts.append((w + 1, h))
if h > 0:
if network[w][h - 1].classType == "Edge":
if network[w][h - 1].answer == "RP":
rootPorts.append((w, h - 1))
if h < networkHeight - 1:
if network[w][h + 1].classType == "Edge":
if network[w][h + 1].answer == "RP":
rootPorts.append((w, h + 1))
if len(rootPorts) > 1:
# print("Removing duplicate root ports, picking lowest port and others will become BP")
lowestPort = (-1, -1)
minPort = 99999
for rp in rootPorts:
if network[rp[0]][rp[1]].port < minPort:
minPort = network[rp[0]][rp[1]].port
lowestPort = rp
for rp in rootPorts:
if rp == lowestPort:
continue
network[rp[0]][rp[1]].SetAnswer("BP")
def RemoveUnlinkedNetworks(networkWidth, networkHeight):
global network
for w in range(networkWidth):
for h in range(networkHeight):
if network[w][h].classType == "Network":
hasActiveEdges = False
if w > 0:
if not network[w - 1][h].IsBlocked():
hasActiveEdges = True
if w < networkWidth - 1:
if not network[w + 1][h].IsBlocked():
hasActiveEdges = True
if h > 0:
if not network[w][h - 1].IsBlocked():
hasActiveEdges = True
if h < networkHeight - 1:
if not network[w][h + 1].IsBlocked():
hasActiveEdges = True
if not hasActiveEdges:
network[w][h] = Empty()
if w > 0:
network[w - 1][h] = Empty()
if w < networkWidth - 1:
network[w + 1][h] = Empty()
if h > 0:
network[w][h - 1] = Empty()
if h < networkHeight - 1:
network[w][h + 1] = Empty()
def RemoveRandomNetworks(networkWidth, networkHeight):
# Removal count
maxRemovalCount = 2
# Find odds to remove a network
totalNetworks = 0
for w in range(networkWidth):
for h in range(networkHeight):
if network[w][h].classType == "Network":
totalNetworks += 1
chance = math.ceil(100.0 / totalNetworks)
# Remove networks
removedCount = 0
for w in range(networkWidth):
if w == 0 or w == networkWidth - 1:
continue
for h in range(networkHeight):
if h == 0 or h == networkHeight - 1:
continue
if network[w][h].classType == "Network":
if random.randint(0, 100) <= chance:
network[w][h] = Empty()
if w > 0:
network[w - 1][h] = Empty()
if w < networkWidth - 1:
network[w + 1][h] = Empty()
if h > 0:
network[w][h - 1] = Empty()
if h < networkHeight - 1:
network[w][h + 1] = Empty()
removedCount += 1
if removedCount >= maxRemovalCount:
return
else:
# Increase the odds
chance *= 3
def GetPaths(aStep, aPreviousSteps, aW, aH, networkWidth, networkHeight):
global rootPaths
global rootW
global rootH
aStep += 1
# Make a new array because python re-uses memory
steps = []
for step in aPreviousSteps:
steps.append(step)
steps.append((aW, aH))
# Stop if we're at the root
if aW == rootW and aH == rootH:
rootPaths.insert(0, steps)
return
# Left
if aW > 0 and (aW - 1, aH) not in steps:
if network[aW - 1][aH].classType != "Empty":
GetPaths(aStep, steps, aW - 1, aH, networkWidth, networkHeight)
# Right
if aW + 1 < networkWidth - 1 and (aW + 1, aH) not in aPreviousSteps:
if network[aW + 1][aH].classType != "Empty":
GetPaths(aStep, steps, aW + 1, aH, networkWidth, networkHeight)
# Up
if aH + 1 < networkHeight - 1 and (aW, aH + 1) not in aPreviousSteps:
if network[aW][aH + 1].classType != "Empty":
GetPaths(aStep, steps, aW, aH + 1, networkWidth, networkHeight)
# Down
if aH > 0 and (aW, aH - 1) not in aPreviousSteps:
if network[aW][aH - 1].classType != "Empty":
GetPaths(aStep, steps, aW, aH - 1, networkWidth, networkHeight)
# Main
def main(args=None):
args = parse_args(args)
if not args.disable_banner:
DrawHeader()
while True:
# Create exercise field
while True:
try:
GenerateField(args.width, args.height)
RemoveRandomNetworks(args.width, args.height)
SolveEdgeLabeling(args.width, args.height)
RemoveUnlinkedNetworks(args.width, args.height)
break
except IndexError:
# Something went wrong in the generation...
# I'll let it retry until it just workTM :^)
pass
# Ask questions
errors = AskRootID(args.width, args.height)
if not args.skip_abbreviations:
errors += AskAbbreviations()
AskEdgeLabeling(
args.width,
args.height,
drawheader=True if not args.disable_banner else False,
errors=errors
)
# Done!
print(colored("All done!", "green"))
inp = input("Would you like to practice again [Y/n]? ").lower()
if inp != "y":
print("Bye, good luck with the exam! :)")
break
def DrawHeader():
print(colored("#######################################################################\n\
# _____ _ _______ #\n\
# / ____| (_) |__ __| #\n\
# | (___ _ __ __ _ _ __ _ __ _ _ __ __ _ | |_ __ ___ ___ #\n\
# \___ \| '_ \ / _` | '_ \| '_ \| | '_ \ / _` | | | '__/ _ \/ _ \ #\n\
# ____) | |_) | (_| | | | | | | | | | | | (_| | | | | | __/ __/ #\n\
# |_____/| .__/ \__,_|_|_|_|_| |_|_|_| |_|\__, | |_|_| \___|\___| #\n\
# | __ \| | | | (_) __/ | #\n\
# | |__) |_|_ __ _ ___| |_ _ ___ ___ _ _|___/ #\n\
# | ___/ '__/ _` |/ __| __| / __|/ _ \ '__| #\n\
# | | | | | (_| | (__| |_| |(__| __/ | #\n\
# |_| |_| \__,_|\___|\__|_\___/\___|_| #\n\
# #\n\
#######################################################################\n\
# Made by Ivar Slotboom :) #\n\
#######################################################################", "magenta")
)
def DrawLabeling():
print("Labeling:")
print(colored("\t[XY]: Bridge, where XY is the ID based on its MAC address and configured priority", "magenta"))
print(colored("\t-XY-: Edge (horizontal), where XY is the port number", "white"))
print(colored("\t|XY|: Edge (vertical), where XY is the port number", "white"))
print(colored("\t{XY}: Network, where XY is the network name", "cyan"))
def AskRootID(networkWidth, networkHeight):
# Root ID
rootID = int(GetRootID(networkWidth, networkHeight))
errors = 0
while True:
print("")
print("Consider the following network:")
DrawField(networkWidth, networkHeight)
while True:
print("")
DrawLabeling()
print("")
try:
rootInputID = int(input("Which bridge is the root (number only)? "))
break
except ValueError:
print("Please only type the number of the root bridge.")
if rootInputID == rootID:
print(colored("Correct!", "green"))
break
else:
errors += 1
print(colored("Incorrect, try again.", "red"))
return errors
def AskAbbreviations():
# Abbreviations
errors = 0
# DP
while True:
inp = input("What does DP stand for? ").lower()
if inp == "designated port":
print(colored("Correct!", "green"))
break
else:
errors += 1
print(colored("Incorrect, try again.", "red"))
# RP
while True:
inp = input("What does RP stand for? ").lower()
if inp == "root port":
print(colored("Correct!", "green"))
break
elif inp == "research project":
print(colored("Well yes, but actually no.", "red"))
else:
errors += 1
print(colored("Incorrect, try again.", "red"))
# BP
while True:
inp = input("What does BP stand for? ").lower()
if inp == "blocked port":
print(colored("Correct!", "green"))
break
else:
errors += 1
print(colored("Incorrect, try again.", "red"))
return errors
__TEST_EDGE__ = False
def AskEdgeLabeling(networkWidth, networkHeight, drawheader=True, errors=0):
# Wait a bit before clearing the screen
time.sleep(1)
if __TEST_EDGE__:
print("PRE:")
print("Root bridge: {}".format(colored("[{}]".format(GetRootID(networkWidth, networkHeight)), "magenta")))
DrawField(networkWidth, networkHeight)
print("POST:")
global highlightW
global highlightH
# Edge labeling
for w in range(networkWidth):
for h in range(networkHeight):
if network[w][h].classType == "Edge":
if __TEST_EDGE__:
network[w][h].SetEdgeType(network[w][h].answer)
else:
# Clear screen on both *nix-like and Windows
try:
os.system('cls')
os.system('clear')
except:
# Ignore OS-relates errors
pass
if drawheader:
DrawHeader()
# Draw the field
print("")
print("In the context of the same network:")
highlightW = w
highlightH = h
DrawField(networkWidth, networkHeight)
print("")
DrawLabeling()
print("")
# Draw field and let user guess
while True:
inp = input("What is the type of the {} edge (RP, DP or BP)? ".format(
colored("highlighted", highlightColor))).upper()
if inp == network[w][h].answer:
print(colored("Correct!", "green"))
network[w][h].SetEdgeType(inp)
time.sleep(1)
highlightW = -1
highlightH = -1
break
else:
errors += 1
print(colored("Incorrect, try again", "red"))
# print("Answer was: {}".format(network[w][h].answer))
print("")
print("Final network:")
DrawField(networkWidth, networkHeight)
print("")
if errors:
print(
colored(
"You had {} error(s) while solving this topology, feel free to try another :)".format(errors),
"red"
)
)
else:
print(colored("Flawless victory! Get ready to pass INR (the STP part at least)", "green"))
if __name__ == "__main__":
# execute only if run as a script
main()