forked from laith43d/TASK1_IP_CLASSIFIER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
191 lines (141 loc) · 5.67 KB
/
main.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
import sys
class ip(object):
_maskList = {
0:'0.0.0.0',
1:'128.0.0.0',
2:'192.0.0.0',
3:'224.0.0.0',
4:'240.0.0.0',
5:'248.0.0.0',
6:'252.0.0.0',
7:'254.0.0.0',
8:'255.0.0.0',
9:'255.128.0.0',
10:'255.192.0.0',
11:'255.224.0.0',
12:'255.240.0.0',
13:'255.248.0.0',
14:'255.252.0.0',
15:'255.254.0.0',
16:'255.255.0.0',
17:'255.255.128.0',
18:'255.255.192.0',
19:'255.255.224.0',
20:'255.255.240.0',
21:'255.255.248.0',
22:'255.255.252.0',
23:'255.255.254.0',
24:'255.255.255.0',
25:'255.255.255.128',
26:'255.255.255.192',
27:'255.255.255.224',
28:'255.255.255.240',
29:'255.255.255.248',
30:'255.255.255.252',
31:'255.255.255.254',
32:'255.255.255.255',
}
def __init__(self, givenIP = ''):
"""
"""
self.workingIP = [0, 0, 0, 0]
self.workingMask = 0
self.workingMaskInt = [0, 0, 0, 0]
# ----------------------------------
self.ipmask = givenIP.split('/')
self.splitMask = self.ipmask[1]
# ---------------------------------
# Split IP segments based on '.'
self.splitIP = self.ipmask[0].split('.')
for index in range(len(self.splitIP)):
self.workingIP[index] = int(self.splitIP[index])
if self.workingIP[index] > 255: raise ValueError
if index > 3: raise ValueError
# --------------------------
# Mask initialization
self.workingMask = int(self.splitMask)
if self.workingMask > 32 or self.workingMask < 0: raise ValueError
if not self.workingMask:
test = self.getClass()
if test == 'A':
self.workingMask = 8
if test == 'B':
self.workingMask = 16
if test == 'C':
self.workingMask = 24
if test == 'D':
print('Multicast Address, using /24')
self.workingMask = 24
if test == 'E':
print('Reserved Address, using /24')
self.workingMask = 24
del test
self.workingMaskListSplit = (self._maskList.get(self.workingMask)).split('.')
self.workingMaskDotted = self._maskList.get(self.workingMask)
for index in range(len(self.workingMaskListSplit)):
self.workingMaskInt[index] = int(self.workingMaskListSplit[index])
def representOutput(self, givenIP):
print(givenIP)
print('Class: {}, Designation: {}'.format(self.getClass(), self.getDesignation()))
def getClass(self):
self.ipClass = ''
test = self.ipBinCalc()
if test[0] == '0':
self.ipClass = 'A'
if test[:2] == '10':
self.ipClass = 'B'
if test[:3] == '110':
self.ipClass = 'C'
if test[:4] == '1110':
self.ipClass = 'D'
if test[:4] == '1111':
self.ipClass = 'E'
return self.ipClass
def ipBinCalc(self):
self.binIP = '{:08b}.{:08b}.{:08b}.{:08b}'.format(self.workingIP[0], self.workingIP[1],
self.workingIP[2], self.workingIP[3])
return self.binIP
def getDesignation(self):
self.ipDesignation = ''
test = self.ipBinCalc()
if test[0] == '0' and self.workingMask == 0:
self.ipDesignation = 'Internet Block'
elif test[0] == '0' and self.workingIP[0] == 10 and self.workingMask >= 8:
self.ipDesignation = 'Internet Private Address'
elif test[0] == '0' and self.workingMask <= 8:
self.ipDesignation = 'Internet Public Address - Supernetting'
elif test[0] == '0' and self.workingIP[0] == 127:
self.ipDesignation = 'Local Host Address'
elif test[:2] == '10' and self.workingIP[0] == 172 and self.workingIP[1] == 16 and self.workingMask >= 12:
self.ipDesignation = 'Internet Private Address'
elif test[:3] == '110' and self.workingIP[0] == 192 and self.workingIP[1] == 168 and self.workingMask >= 16:
self.ipDesignation = 'Internet Private Address'
else:
self.ipDesignation = 'Internet Public Address'
return self.ipDesignation
def main():
if len(sys.argv) == 1:
givenIP = input('\nPlease give an IP address (IP/MASK), (to exit type "X"): ')
try:
ipAddress = ip(givenIP)
ipAddress.representOutput(givenIP)
except (IndexError, ValueError):
if givenIP == 'x' or givenIP == 'X': sys.exit()
else:
print('\nPlease give a valid IPv4/MASK address in #.#.#.#/# format,' '\n'
'\tfor example: 192.168.1.1/24, or 10.10.10.1/28')
main()
else:
print("This version accepts only one IP address, processing the first give IP - \n")
givenIP = sys.argv[1]
try:
ipAddress = ip(givenIP)
ipAddress.representOutput(givenIP)
except (IndexError, ValueError):
if givenIP == 'x' or givenIP == 'X': sys.exit()
else:
print('\nPlease give a valid IPv4/MASK address in #.#.#.#/# format,' '\n'
'\tfor example: 192.168.1.1/24, or 10.10.10.1/28')
if __name__ == '__main__':
pass
main()