def testInt2Ip(self): from Exscript.util.ipv4 import int2ip, ip2int for ip in ('0.0.0.0', '255.255.255.255', '255.255.255.0', '0.255.255.0', '0.128.255.0'): self.assertEqual(int2ip(ip2int(ip)), ip)
def testPfxlen2MaskInt(self): from Exscript.util.ipv4 import pfxlen2mask_int, int2ip self.assertEqual(int2ip(pfxlen2mask_int(32)), '255.255.255.255') self.assertEqual(int2ip(pfxlen2mask_int(31)), '255.255.255.254') self.assertEqual(int2ip(pfxlen2mask_int(30)), '255.255.255.252') self.assertEqual(int2ip(pfxlen2mask_int(2)), '192.0.0.0') self.assertEqual(int2ip(pfxlen2mask_int(1)), '128.0.0.0') self.assertEqual(int2ip(pfxlen2mask_int(0)), '0.0.0.0')
def mask(scope, ips, mask): """ Applies the given IP mask (e.g. 255.255.255.0) to the given IP address (or list of IP addresses) and returns it. :type ips: string :param ips: A prefix, or a list of IP prefixes. :type mask: string :param mask: An IP mask. :rtype: string :return: The network(s) that result(s) from applying the mask. """ mask = ipv4.ip2int(mask[0]) return [ipv4.int2ip(ipv4.ip2int(ip) & mask) for ip in ips]
def pfxmask(scope, ips, pfxlen): """ Applies the given prefix length to the given ips, resulting in a (list of) IP network addresses. :type ips: string :param ips: An IP address, or a list of IP addresses. :type pfxlen: int :param pfxlen: An IP prefix length. :rtype: string :return: The mask(s) that result(s) from converting the prefix length. """ mask = ipv4.pfxlen2mask_int(pfxlen[0]) return [ipv4.int2ip(ipv4.ip2int(ip) & mask) for ip in ips]
def mask(scope, ips, mask): """ Applies the given IP mask (e.g. 255.255.255.0) to the given IP address (or list of IP addresses) and returns it. @type ips: string @param ips: A prefix, or a list of IP prefixes. @type mask: string @param mask: An IP mask. @rtype: string @return: The network(s) that result(s) from applying the mask. """ mask = ipv4.ip2int(mask[0]) return [ipv4.int2ip(ipv4.ip2int(ip) & mask) for ip in ips]
def pfxmask(scope, ips, pfxlen): """ Applies the given prefix length to the given ips, resulting in a (list of) IP network addresses. @type ips: string @param ips: An IP address, or a list of IP addresses. @type pfxlen: int @param pfxlen: An IP prefix length. @rtype: string @return: The mask(s) that result(s) from converting the prefix length. """ mask = ipv4.pfxlen2mask_int(pfxlen[0]) return [ipv4.int2ip(ipv4.ip2int(ip) & mask) for ip in ips]