I want to check if IP 180.179.77.11
lies between a particular range, say 180.179.0.0 - 180.179.255.255
.
I wrote a function which will compare every IP octet with the others.
def match(mask, IP):
min_ip = mask.split(' - ')[0].split('.')
max_ip = mask.split(' - ')[1].split('.')
ip = IP.split('.')
for i in range(4):
print ip[i] + " < " + min_ip[i] + " or " + ip[i] + " > " + max_ip[i]
print ip[i] < min_ip[i] or ip[i] > max_ip[i]
if ip[i] < min_ip[i] or ip[i] > max_ip[i]:
return False
return True
match("180.179.0.0 - 180.179.255.255", "180.179.77.11")
OUTPUT:
180 < 180 or 180 > 180
False
179 < 179 or 179 > 179
False
77 < 0 or 77 > 255 # 77 > 255 is true
True
False
However, it doesn't seem to work properly; It looks like the problem occurred in the 3rd octet while comparing 77
with 255
.
I have put some print statements to illustrate the problem area.