示例#1
0
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']]
示例#2
0
    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))
示例#3
0
    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))
示例#4
0
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
示例#5
0
    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'])
示例#6
0
    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'])
示例#7
0
    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'
            }]
        )
示例#8
0
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
示例#9
0
    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'
            }]
        )
示例#10
0
    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']
        )
示例#11
0
     },
     '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')