def _network(left, right): 'Helper for match_spec.' if _HAS_IPADDR: return ipaddr.IPv4Address(left) in ipaddr.IPv4Network(right) else: return False
def is_ip4_address(attribute, value): try: ipaddr.IPv4Address(value) except ipaddr.AddressValueError: return False return True
def __contains__(self, ip): return set.__contains__(self, ipaddr.IPv4Address(ip))
def gen_rondomip(): network = ipaddr.IPv4Network('100.0.0.0/16') return ipaddr.IPv4Address( random.randrange(int(network.network) + 1, int(network.broadcast) - 1))
def _is_ip4(value): try: ipaddr.IPv4Address(value) return True except ipaddr.AddressValueError: return False
def add(self, ip): set.add(self, ipaddr.IPv4Address(ip))
def testCollapsing(self): # test only IP addresses including some duplicates ip1 = ipaddr.IPv4Address('1.1.1.0') ip2 = ipaddr.IPv4Address('1.1.1.1') ip3 = ipaddr.IPv4Address('1.1.1.2') ip4 = ipaddr.IPv4Address('1.1.1.3') ip5 = ipaddr.IPv4Address('1.1.1.4') ip6 = ipaddr.IPv4Address('1.1.1.0') # check that addreses are subsumed properly. collapsed = ipaddr.collapse_address_list( [ip1, ip2, ip3, ip4, ip5, ip6]) self.assertEqual(collapsed, [ ipaddr.IPv4Network('1.1.1.0/30'), ipaddr.IPv4Network('1.1.1.4/32') ]) # test a mix of IP addresses and networks including some duplicates ip1 = ipaddr.IPv4Address('1.1.1.0') ip2 = ipaddr.IPv4Address('1.1.1.1') ip3 = ipaddr.IPv4Address('1.1.1.2') ip4 = ipaddr.IPv4Address('1.1.1.3') ip5 = ipaddr.IPv4Network('1.1.1.4/30') ip6 = ipaddr.IPv4Network('1.1.1.4/30') # check that addreses are subsumed properly. collapsed = ipaddr.collapse_address_list( [ip5, ip1, ip2, ip3, ip4, ip6]) self.assertEqual(collapsed, [ipaddr.IPv4Network('1.1.1.0/29')]) # test only IP networks ip1 = ipaddr.IPv4Network('1.1.0.0/24') ip2 = ipaddr.IPv4Network('1.1.1.0/24') ip3 = ipaddr.IPv4Network('1.1.2.0/24') ip4 = ipaddr.IPv4Network('1.1.3.0/24') ip5 = ipaddr.IPv4Network('1.1.4.0/24') # stored in no particular order b/c we want CollapseAddr to call [].sort ip6 = ipaddr.IPv4Network('1.1.0.0/22') # check that addreses are subsumed properly. collapsed = ipaddr.collapse_address_list( [ip1, ip2, ip3, ip4, ip5, ip6]) self.assertEqual(collapsed, [ ipaddr.IPv4Network('1.1.0.0/22'), ipaddr.IPv4Network('1.1.4.0/24') ]) # test that two addresses are supernet'ed properly collapsed = ipaddr.collapse_address_list([ip1, ip2]) self.assertEqual(collapsed, [ipaddr.IPv4Network('1.1.0.0/23')]) # test same IP networks ip_same1 = ip_same2 = ipaddr.IPv4Network('1.1.1.1/32') self.assertEqual(ipaddr.collapse_address_list([ip_same1, ip_same2]), [ip_same1]) # test same IP addresses ip_same1 = ip_same2 = ipaddr.IPv4Address('1.1.1.1') self.assertEqual(ipaddr.collapse_address_list([ip_same1, ip_same2]), [ipaddr.IPNetwork('1.1.1.1/32')]) ip1 = ipaddr.IPv6Network('::2001:1/100') ip2 = ipaddr.IPv6Network('::2002:1/120') ip3 = ipaddr.IPv6Network('::2001:1/96') # test that ipv6 addresses are subsumed properly. collapsed = ipaddr.collapse_address_list([ip1, ip2, ip3]) self.assertEqual(collapsed, [ip3]) # the toejam test ip1 = ipaddr.IPAddress('1.1.1.1') ip2 = ipaddr.IPAddress('::1') self.assertRaises(TypeError, ipaddr.collapse_address_list, [ip1, ip2])
def test_isIPv6_randomIP4(self): """Test :func:`addr.isIPv6` with a random IPv4 address.""" randomAddr = ipaddr.IPv4Address(random.getrandbits(32)).compressed log.msg("Testing randomly generated IPv4 address: %s" % randomAddr) self.runTestForIPv4(randomAddr)
def testsixtofour(self): sixtofouraddr = ipaddr.IPAddress('2002:ac1d:2d64::1') bad_addr = ipaddr.IPAddress('2000:ac1d:2d64::1') self.assertEqual(ipaddr.IPv4Address('172.29.45.100'), sixtofouraddr.sixtofour) self.assertFalse(bad_addr.sixtofour)
def testIterHosts(self): self.assertEqual( [ipaddr.IPv4Address('2.0.0.0'), ipaddr.IPv4Address('2.0.0.1')], list(ipaddr.IPNetwork('2.0.0.0/31').iterhosts()))
def is_ipv4addr(addr): try: ipaddr.IPv4Address(addr) except ValueError: return False return True
def ipv6_to_ipv4(ipv6): hashed = hash(ipv6) & 0xfffffff ip = ipaddr.IPv4Address(hashed) return ip.compressed
return 'export' def backup(self, backup_root): dst_dir = HostControl.backup(self, backup_root) fileb = self._backup_binary(dst_dir) filec = self._backup_config(dst_dir) return '%s+%s' % (fileb, filec) def reboot(self, tick): return HostControl.reboot(self, 'system reboot ; beep', 10, tick) if __name__ == '__main__': if len(sys.argv) < 3: sys.exit('usage: %s ipaddress password [reboot]' % sys.argv[0]) ip = ipaddr.IPv4Address(sys.argv[1]) pwd = sys.argv[2] reboot = (len(sys.argv) > 3 and sys.argv[3] == 'reboot') router = MikrotikRouter('MyRouter', ip, pwd) if not router.is_pingable(): sys.exit('CANNOT PING') print 'Version =', router.get_version() print 'Hardware =', router.get_hardware() print 'Uptime about', crawler_util.rough_timespan(router.get_uptime()) # print 'Adjacency:' # print router.get_adjacency() if reboot: print router.backup('.') print router.reboot(True)
def FormatIP(packed_ip): try: ip_address = ipaddr.IPv4Address(packed_ip) except ipaddr.AddressValueError as e: raise bigquery_client.BigqueryInvalidQueryError(e, None, None, None) return str(ip_address)
def testInvalidStrings(self): def AssertInvalidIP(ip_str): self.assertRaises(ValueError, ipaddr.IPAddress, ip_str) AssertInvalidIP("") AssertInvalidIP("016.016.016.016") AssertInvalidIP("016.016.016") AssertInvalidIP("016.016") AssertInvalidIP("016") AssertInvalidIP("000.000.000.000") AssertInvalidIP("000") AssertInvalidIP("0x0a.0x0a.0x0a.0x0a") AssertInvalidIP("0x0a.0x0a.0x0a") AssertInvalidIP("0x0a.0x0a") AssertInvalidIP("0x0a") AssertInvalidIP("42.42.42.42.42") AssertInvalidIP("42.42.42") AssertInvalidIP("42.42") AssertInvalidIP("42") AssertInvalidIP("42..42.42") AssertInvalidIP("42..42.42.42") AssertInvalidIP("42.42.42.42.") AssertInvalidIP("42.42.42.42...") AssertInvalidIP(".42.42.42.42") AssertInvalidIP("...42.42.42.42") AssertInvalidIP("42.42.42.-0") AssertInvalidIP("42.42.42.+0") AssertInvalidIP(".") AssertInvalidIP("...") AssertInvalidIP("bogus") AssertInvalidIP("bogus.com") AssertInvalidIP("192.168.0.1.com") AssertInvalidIP("12345.67899.-54321.-98765") AssertInvalidIP("257.0.0.0") AssertInvalidIP("42.42.42.-42") AssertInvalidIP("3ffe::1.net") AssertInvalidIP("3ffe::1::1") AssertInvalidIP("1::2::3::4:5") AssertInvalidIP("::7:6:5:4:3:2:") AssertInvalidIP(":6:5:4:3:2:1::") AssertInvalidIP("2001::db:::1") AssertInvalidIP("FEDC:9878") AssertInvalidIP("+1.+2.+3.4") AssertInvalidIP("1.2.3.4e0") AssertInvalidIP("::7:6:5:4:3:2:1:0") AssertInvalidIP("7:6:5:4:3:2:1:0::") AssertInvalidIP("9:8:7:6:5:4:3::2:1") AssertInvalidIP("0:1:2:3::4:5:6:7") AssertInvalidIP("3ffe:0:0:0:0:0:0:0:1") AssertInvalidIP("3ffe::10000") AssertInvalidIP("3ffe::goog") AssertInvalidIP("3ffe::-0") AssertInvalidIP("3ffe::+0") AssertInvalidIP("3ffe::-1") AssertInvalidIP(":") AssertInvalidIP(":::") AssertInvalidIP("::1.2.3") AssertInvalidIP("::1.2.3.4.5") AssertInvalidIP("::1.2.3.4:") AssertInvalidIP("1.2.3.4::") AssertInvalidIP("2001:db8::1:") AssertInvalidIP(":2001:db8::1") AssertInvalidIP(":1:2:3:4:5:6:7") AssertInvalidIP("1:2:3:4:5:6:7:") AssertInvalidIP(":1:2:3:4:5:6:") AssertInvalidIP("192.0.2.1/32") AssertInvalidIP("2001:db8::1/128") self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv4Network, '') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv4Network, 'google.com') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv4Network, '::1.2.3.4') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv6Network, '') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv6Network, 'google.com') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv6Network, '1.2.3.4') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv6Network, 'cafe:cafe::/128/190') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv6Network, '1234:axy::b') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv6Address, '1234:axy::b') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv6Address, '2001:db8:::1') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv6Address, '2001:888888::1') self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv4Address(1)._ip_int_from_string, '1.a.2.3') self.assertEqual(False, ipaddr.IPv4Network(1)._is_hostmask('1.a.2.3'))
def ParseIP(readable_ip): try: ip_address = ipaddr.IPv4Address(readable_ip) except ipaddr.AddressValueError as e: raise bigquery_client.BigqueryInvalidQueryError(e, None, None, None) return int(ip_address)
def is_in_network(left, right): 'Helper for match_spec.' return ipaddr.IPv4Address(left) in ipaddr.IPv4Network(right)
if not rules_plus and not rules_minus: return print "+++", name_plus print "---", name_minus filters = ([(True, rule) for rule in rules_plus if rule.type == 'FILTER'] + [(False, rule) for rule in rules_minus if rule.type == 'FILTER']) nats = ([(True, rule) for rule in rules_plus if rule.type == 'NAT'] + [(False, rule) for rule in rules_minus if rule.type == 'NAT']) if filters: display_table(filters, projection=['diff'] + filter_projection if not projection else ['diff'] + projection) if nats: display_table(nats, projection=['diff'] + projection if projection else []) if __name__ == '__main__': # TESTS print rewrite_with_aliases( [(ipaddr.IPv4Address('10.0.0.0'), ipaddr.IPv4Address('10.0.2.255')), (ipaddr.IPv4Address('127.0.0.1'), ipaddr.IPv4Address('127.0.0.1'))], { 'lan0': '10.0.1.0/24', 'lan1': '10.0.2.0/24' })
def test_getRemoteIP_useConfiguredIP(self): """Check that our remoteip setting is used if configured.""" ip = self.captchaResource.getRemoteIP() realishIP = ipaddr.IPv4Address(ip).compressed self.assertTrue(realishIP) self.assertEquals(realishIP, '111.111.111.111')