|
|
@ -62,8 +62,8 @@ def checkNet(net):
|
|
|
|
"""Check net"""
|
|
|
|
"""Check net"""
|
|
|
|
if not reNet.match(net):
|
|
|
|
if not reNet.match(net):
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
mask = strIpToIntIp(netToMask(int(net)))
|
|
|
|
ip,op,cidr = net.partition('/')
|
|
|
|
ip,op,net = net.partition('/')
|
|
|
|
mask = strIpToIntIp(cidrToMask(int(cidr)))
|
|
|
|
return (strIpToIntIp(ip)&mask) == (strIpToIntIp(ip))
|
|
|
|
return (strIpToIntIp(ip)&mask) == (strIpToIntIp(ip))
|
|
|
|
|
|
|
|
|
|
|
|
maskDigs = map(lambda x:str(x),(0b10000000,0b11000000,0b11100000,0b11110000,
|
|
|
|
maskDigs = map(lambda x:str(x),(0b10000000,0b11000000,0b11100000,0b11110000,
|
|
|
@ -106,19 +106,23 @@ def intIpToStrIp(addr):
|
|
|
|
return "{0}.{1}.{2}.{3}".format(
|
|
|
|
return "{0}.{1}.{2}.{3}".format(
|
|
|
|
addr>>24,(addr>>16)&0xff,(addr>>8)&0xff,addr&0xff)
|
|
|
|
addr>>24,(addr>>16)&0xff,(addr>>8)&0xff,addr&0xff)
|
|
|
|
|
|
|
|
|
|
|
|
def maskToNet(mask):
|
|
|
|
def maskToCidr(mask):
|
|
|
|
"""Convert mask specified by str to net"""
|
|
|
|
"""Convert mask specified by str to net"""
|
|
|
|
mask = strIpToIntIp(mask)
|
|
|
|
mask = strIpToIntIp(mask)
|
|
|
|
return 32-int(math.log(((~mask) & 0xffffffff)+1,2))
|
|
|
|
return 32-int(math.log(((~mask) & 0xffffffff)+1,2))
|
|
|
|
|
|
|
|
|
|
|
|
def netToMask(net):
|
|
|
|
def cidrToMask(cidr):
|
|
|
|
"""Convert net to mask specified by str"""
|
|
|
|
"""Convert net to mask specified by str"""
|
|
|
|
return intIpToStrIp((2**net-1)<<(32-net))
|
|
|
|
return intIpToStrIp((2**cidr-1)<<(32-cidr))
|
|
|
|
|
|
|
|
|
|
|
|
def getIpNet(ip,mask):
|
|
|
|
def getIpNet(ip,mask=None,cidr=None):
|
|
|
|
"""Get net (xx.xx.xx.xx/xx) by ip address and mask"""
|
|
|
|
"""Get net (xx.xx.xx.xx/xx) by ip address and mask"""
|
|
|
|
ip = strIpToIntIp(ip)
|
|
|
|
ip = strIpToIntIp(ip)
|
|
|
|
net = maskToNet(mask)
|
|
|
|
if not mask is None:
|
|
|
|
|
|
|
|
net = maskToCidr(mask)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
net = int(cidr)
|
|
|
|
|
|
|
|
mask = cidrToMask(net)
|
|
|
|
mask = strIpToIntIp(mask)
|
|
|
|
mask = strIpToIntIp(mask)
|
|
|
|
return "{ip}/{net}".format(ip=intIpToStrIp(ip&mask),
|
|
|
|
return "{ip}/{net}".format(ip=intIpToStrIp(ip&mask),
|
|
|
|
net=net)
|
|
|
|
net=net)
|
|
|
@ -127,7 +131,7 @@ def isIpInNet(checkip,*ipnets):
|
|
|
|
"""Check is ip in specified nets"""
|
|
|
|
"""Check is ip in specified nets"""
|
|
|
|
return map(lambda x:x[0],
|
|
|
|
return map(lambda x:x[0],
|
|
|
|
filter(lambda x:strIpToIntIp(checkip)&x[2] == strIpToIntIp(x[1])&x[2],
|
|
|
|
filter(lambda x:strIpToIntIp(checkip)&x[2] == strIpToIntIp(x[1])&x[2],
|
|
|
|
map(lambda x:(x[0],x[1][0],strIpToIntIp(netToMask(int(x[1][1])))),
|
|
|
|
map(lambda x:(x[0],x[1][0],strIpToIntIp(cidrToMask(int(x[1][1])))),
|
|
|
|
map(lambda x:(x,x.partition('/')[0::2]),
|
|
|
|
map(lambda x:(x,x.partition('/')[0::2]),
|
|
|
|
ipnets))))
|
|
|
|
ipnets))))
|
|
|
|
|
|
|
|
|
|
|
|