0

Possible Duplicate:
How can I check if an ip is in a network in python

I have one destination ip address from a file suppose ip1 = 172.16.0.34; and I have one more file having (subnet, mask, next-hop) i.e 172.16.0.0, 255.255.255.0, 117.120.10.1

I have to check whether the incoming address ip1 matches the subnet entry for a particular route,after being ANDed with the netmask of that entry in the routing table. If the destination matches under mask,add the route to the list of feasible routes for this packet

Community
  • 1
  • 1

2 Answers2

0

I'm assuming their strings to begin with:

ipadd = '192.168.1.1'
mask = '255.255.0.0'
anded = list()
for ip, m in zip(ipadd.split('.'),mask.split('.')):
   anded.append(str(int(ip) & int(m)))
subnet = '.'.join(anded)
Stephen
  • 2,365
  • 17
  • 21
0

Try putting them in a Array then compare ! it's just my suggestion ...

F25rT
  • 114
  • 1
  • 10