2019-01-08 12:40:07 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
try:
|
|
|
|
import simplejson as json
|
|
|
|
except ImportError:
|
|
|
|
import json
|
|
|
|
|
|
|
|
from urllib.parse import urljoin
|
|
|
|
from pybgpranking import BGPRanking
|
|
|
|
from pyipasnhistory import IPASNHistory
|
|
|
|
from datetime import date, timedelta
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(description='Run a query against BGP Ranking')
|
|
|
|
parser.add_argument('--url', type=str, help='URL of the instance.')
|
2019-07-22 16:28:58 +02:00
|
|
|
parser.add_argument('--date', default=date.today().isoformat(), help='Date of the dataset required')
|
2019-01-08 12:40:07 +01:00
|
|
|
|
2019-07-22 16:28:58 +02:00
|
|
|
sub_parsers = parser.add_subparsers(title='Available commands')
|
|
|
|
|
|
|
|
index_query = sub_parsers.add_parser('index')
|
|
|
|
index_query.add_argument('--limit', default=100, help='Max number of ASN to get')
|
|
|
|
index_query.add_argument('--family', default='v4', help='v4 or v6')
|
|
|
|
index_query.set_defaults(which='index')
|
|
|
|
|
|
|
|
simple_query = sub_parsers.add_parser('simple')
|
|
|
|
group = simple_query.add_mutually_exclusive_group(required=True)
|
2019-01-08 12:40:07 +01:00
|
|
|
group.add_argument('--asn', help='ASN to lookup')
|
|
|
|
group.add_argument('--ip', help='IP to lookup')
|
2019-07-22 16:28:58 +02:00
|
|
|
simple_query.set_defaults(which='simple')
|
2019-01-08 12:40:07 +01:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.url:
|
|
|
|
bgpranking = BGPRanking(args.url)
|
|
|
|
ipasn = IPASNHistory(urljoin(args.url, 'ipasn_history'))
|
|
|
|
else:
|
|
|
|
bgpranking = BGPRanking()
|
|
|
|
ipasn = IPASNHistory()
|
|
|
|
|
2019-07-22 16:28:58 +02:00
|
|
|
if args.which == 'simple':
|
|
|
|
if args.ip:
|
|
|
|
response = ipasn.query(args.ip)
|
|
|
|
print(json.dumps(response, indent=2))
|
|
|
|
if 'response' in response and response['response']:
|
|
|
|
asn = response['response'][list(response['response'].keys())[0]]['asn']
|
|
|
|
else:
|
|
|
|
asn = args.asn
|
|
|
|
|
|
|
|
response = bgpranking.query(asn, date=(date.today() - timedelta(1)).isoformat())
|
|
|
|
elif args.which == 'index':
|
|
|
|
response = bgpranking.asns_global_ranking(address_family=args.family, limit=args.limit, date=args.date)
|
2019-01-08 12:40:07 +01:00
|
|
|
print(json.dumps(response, indent=2))
|