-
Notifications
You must be signed in to change notification settings - Fork 0
/
Subnet_Visualizer.py
70 lines (57 loc) · 1.76 KB
/
Subnet_Visualizer.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
import sys
from prettytable import PrettyTable
def toBin(dec):
bin = ''
for i in range(0,8):
bin += str(dec % 2)
dec = dec // 2
bin += ' '
return bin[::-1]
def invalidIP(address):
print(address + " is not a valid IP address...")
sys.exit(0)
def IpToBin(address):
numbers = str.split(address,'.')
if(not(len(numbers) == 4)):
invalidIP(address)
bin = ''
for num in numbers:
try:
if(int(num) < 0 or int(num) > 255):
invalidIP(address)
else:
bin += toBin(int(num))
except:
invalidIP(address)
return bin
def checkSubnet(first,second,subnet):
match=''
for i in range(0,len(first)):
if(first[i] == ' '):
match += " "
elif((subnet[i] == '1' and (first[i] == second[i] or first == ' '))):
match += '_'
elif(subnet[i] == '0'):
match += '*'
else:
match += 'X'
return match
firstIP = input("Enter the First IP address: ")
firstBin= IpToBin(firstIP)
secondIP = input("Enter the Second IP Address: ")
secondBin= IpToBin(secondIP)
subnetMask = input("Enter the Subnet Mask:" )
subnetBin = IpToBin(subnetMask)
check = checkSubnet(firstBin,secondBin,subnetBin)
same = "yes"
if('X' in check):
same = "no"
table = PrettyTable([' ','Ip Address','Binary'])
table.align = "l"
table.add_row(['First IP',firstIP,firstBin])
table.add_row(['Second IP',secondIP,secondBin])
table.add_row(['Subnet Mask',subnetMask,subnetBin])
table.align = "c"
table.add_row(['--------------','--------------','---------------------------------'])
table.add_row(['Same Subnet ?', same ,check])
print (table)