def getAllroutesOld(asn): from ipwhois.net import Net from ipwhois.asn import ASNOrigin as ASNOriginCls net = Net('108.160.160.0') obj = ASNOriginCls(net) results = obj.lookup(asn) return [ipaddress.ip_network(_net['cidr']) for _net in results['nets']]
def test__ASNOriginLookup(self): data_dir = path.dirname(__file__) with io.open(str(data_dir) + '/asn.json', 'r') as \ data_file: data = json.load(data_file) for key, val in data.items(): log.debug('Testing: {0}'.format(key)) net = Net(key) obj = ASNOrigin(net) try: self.assertIsInstance(obj.lookup(asn=val['asn'], inc_raw=True, response=val['response']), dict) except AssertionError as e: raise e except Exception as e: self.fail('Unexpected exception raised: {0}'.format(e))
def test__ASNOriginLookup(self): data_dir = path.dirname(__file__) with io.open(str(data_dir) + '/asn.json', 'r') as \ data_file: data = json.load(data_file) for key, val in data.items(): log.debug('Testing: {0}'.format(key)) net = Net(key) obj = ASNOrigin(net) try: self.assertIsInstance(obj.lookup(asn=val['asn'], inc_raw=True, response=val['response']), dict) except AssertionError as e: raise e except Exception as e: self.fail('Unexpected exception raised: {0}'.format(e))
def get_networks_from_asn(asn_list): net = Net('2001:43f8:7b0::') asn_origin = ASNOrigin(net) networks = [] for asn in ASN_LIST: nets = asn_origin.lookup(asn, field_list=['source'])['nets'] networks.extend(ipaddress.ip_network(net['cidr']) for net in nets) return networks
def test__parse_fields(self): net = Net('74.125.225.229') obj = ASNOrigin(net) # No exception raised, but should provide code coverage for if regex # groups are messed up. tmp_dict = ASN_ORIGIN_WHOIS['radb']['fields'] tmp_dict['route'] = r'(route):[^\S\n]+(?P<val1>.+?)\n' obj._parse_fields(response="\nroute: 66.249.64.0/20\n", fields_dict=tmp_dict) obj._parse_fields( response="\nchanged: [email protected] 20110301\n", fields_dict=ASN_ORIGIN_WHOIS['radb']['fields']) multi_net_response = ('\n\nroute: 66.249.64.0/20' '\ndescr: Google' '\norigin: AS15169' '\nnotify: [email protected]' '\nmnt-by: MAINT-AS15169' '\nchanged: [email protected] 20110301' '\nsource: RADB' '\n\nroute: 66.249.80.0/20' '\ndescr: Google' '\norigin: AS15169' '\nnotify: [email protected]' '\nmnt-by: MAINT-AS15169' '\nchanged: [email protected] 20110301' '\nsource: RADB' '\n\n') obj._parse_fields(response=multi_net_response, fields_dict=ASN_ORIGIN_WHOIS['radb']['fields'])
def test__TestASNOriginLookup(self): data_dir = path.abspath(path.join(path.dirname(__file__), '..')) with io.open(str(data_dir) + '/asn.json', 'r') as \ data_file: data = json.load(data_file) # IP doesn't matter here net = Net('74.125.225.229') for key, val in data.items(): log.debug('Testing: {0} - {1}'.format(key, val['asn'])) obj = ASNOrigin(net) try: self.assertIsInstance( obj.lookup( asn=val['asn'] ), dict ) except ASNOriginLookupError: pass except AssertionError as e: raise e except Exception as e: self.fail('Unexpected exception raised: {0}'.format(e)) net = Net(address='74.125.225.229', timeout=0, allow_permutations=True) asnorigin = ASNOrigin(net) self.assertRaises(ASNOriginLookupError, asnorigin.lookup, **dict( asn='15169', asn_alts=['http'])) self.assertRaises(ValueError, asnorigin.lookup, **dict( asn='15169', asn_methods=['asd'])) net = Net(address='74.125.225.229') asnorigin = ASNOrigin(net) asnorigin.lookup(asn='15169', asn_methods=['whois', 'http'])
def test__get_nets_radb(self): net = Net('74.125.225.229') obj = ASNOrigin(net) # No exception raised, but should provide code coverage for multiple # network scenarios and CIDR invalid IP ValueError. multi_net_response = ( '\n\nroute: 66.249.64.0/20' '\ndescr: Google' '\norigin: AS15169' '\nnotify: [email protected]' '\nmnt-by: MAINT-AS15169' '\nchanged: [email protected] 20110301' '\nsource: RADB' '\n\nroute: 66.249.80.0/20' '\ndescr: Google' '\norigin: AS15169' '\nnotify: [email protected]' '\nmnt-by: MAINT-AS15169' '\nchanged: [email protected] 20110301' '\nsource: RADB' '\n\n' ) obj._get_nets_radb(multi_net_response) self.assertEqual(obj._get_nets_radb(multi_net_response, is_http=True), []) net = Net('2001:43f8:7b0::') obj = ASNOrigin(net) multi_net_response = ( '\n\nroute6: 2001:43f8:7b0::/48' '\ndescr: KIXP Nairobi Management Network' '\norigin: AS37578' '\norg: ORG-TA38-AFRINIC' '\nmnt-by: TESPOK-MNT' '\nchanged: ***@isoc.org 20160721' '\nsource: AFRINIC' '\n\n' ) self.assertEquals( obj._get_nets_radb(multi_net_response), [{ 'updated': None, 'maintainer': None, 'description': None, 'start': 2, 'source': None, 'end': 36, 'cidr': '2001:43f8:7b0::/48' }] )
def get_cidr(ip, asn): try: x = [] cidr = ASNOrigin(Net(ip)).lookup(asn=str(asn), retry_count=10, asn_methods=['whois']) if cidr and len(cidr['nets']) > 0: for c in cidr['nets']: x.append(c['cidr']) return x except ASNOriginLookupError: pass
def test__get_nets_radb(self): net = Net('74.125.225.229') obj = ASNOrigin(net) # No exception raised, but should provide code coverage for multiple # network scenarios and CIDR invalid IP ValueError. multi_net_response = ( '\n\nroute: 66.249.64.0/20' '\ndescr: Google' '\norigin: AS15169' '\nnotify: [email protected]' '\nmnt-by: MAINT-AS15169' '\nchanged: [email protected] 20110301' '\nsource: RADB' '\n\nroute: 66.249.80.0/20' '\ndescr: Google' '\norigin: AS15169' '\nnotify: [email protected]' '\nmnt-by: MAINT-AS15169' '\nchanged: [email protected] 20110301' '\nsource: RADB' '\n\n' ) obj.get_nets_radb(multi_net_response) self.assertEqual(obj.get_nets_radb(multi_net_response, is_http=True), []) net = Net('2001:43f8:7b0::') obj = ASNOrigin(net) multi_net_response = ( '\n\nroute6: 2001:43f8:7b0::/48' '\ndescr: KIXP Nairobi Management Network' '\norigin: AS37578' '\norg: ORG-TA38-AFRINIC' '\nmnt-by: TESPOK-MNT' '\nchanged: ***@isoc.org 20160721' '\nsource: AFRINIC' '\n\n' ) self.assertEquals( obj.get_nets_radb(multi_net_response), [{ 'updated': None, 'maintainer': None, 'description': None, 'start': 2, 'source': None, 'end': 36, 'cidr': '2001:43f8:7b0::/48' }] )
def test_parse_fields(self): net = Net('74.125.225.229') obj = ASNOrigin(net) # No exception raised, but should provide code coverage for if regex # groups are messed up. tmp_dict = ASN_ORIGIN_WHOIS['radb']['fields'] tmp_dict['route'] = r'(route):[^\S\n]+(?P<val1>.+?)\n' obj.parse_fields( response="\nroute: 66.249.64.0/20\n", fields_dict=tmp_dict ) obj.parse_fields( response="\nchanged: [email protected] 20110301\n", fields_dict=ASN_ORIGIN_WHOIS['radb']['fields'] ) multi_net_response = ( '\n\nroute: 66.249.64.0/20' '\ndescr: Google' '\norigin: AS15169' '\nnotify: [email protected]' '\nmnt-by: MAINT-AS15169' '\nchanged: [email protected] 20110301' '\nsource: RADB' '\n\nroute: 66.249.80.0/20' '\ndescr: Google' '\norigin: AS15169' '\nnotify: [email protected]' '\nmnt-by: MAINT-AS15169' '\nchanged: [email protected] 20110301' '\nsource: RADB' '\n\n' ) obj.parse_fields( response=multi_net_response, fields_dict=ASN_ORIGIN_WHOIS['radb']['fields'] )
}, 'OUTPUT_ASN_ORIGIN_BASIC': { 'content': ( '::\n\n' ' >>>> from ipwhois.net import Net\n' ' >>>> from ipwhois.asn import ASNOrigin\n' ' >>>> from pprint import pprint\n\n' ' >>>> net = Net(\'2001:43f8:7b0::\')\n' ' >>>> obj = ASNOrigin(net)\n' ' >>>> results = obj.lookup(asn=\'AS37578\')\n' ' >>>> pprint(results)\n\n' ' {0}' ), 'queries': { '0': lambda: ASNOrigin(Net('2001:43f8:7b0::')).lookup( asn='AS37578' ), } } }, 'EXPERIMENTAL.rst': { 'GET_BULK_ASN_WHOIS_OUTPUT_BASIC': { 'content': ( '::\n\n' ' >>>> from ipwhois.experimental import get_bulk_asn_whois' '\n' ' >>>> from pprint import pprint\n\n' ' >>>> ip_list = [\'74.125.225.229\', ' '\'2001:4860:4860::8888\', \'62.239.237.1\', ' '\'2a00:2381:ffff::1\', \'210.107.73.73\', ' '\'2001:240:10c:1::ca20:9d1d\', \'200.57.141.161\', '
def __get_asn_nets(self, ip): asn_origin = self.__get_asn_origin(ip) origin = ASNOrigin(Net(ip)) lookup = origin.lookup(asn='AS{}'.format(asn_origin), asn_methods=['whois']) return lookup.get('nets')