def get_asn_prefixes(asn): db = myStats.db prefixes = [] if asn == C.DEFAULT_ASN: routes = db.bgp.find({'origin_asn': None, 'active': True}) else: routes = db.bgp.find({'origin_asn': asn, 'active': True}) for prefix in routes: prefixes.append({ 'prefix': prefix['_id'], 'is_transit': is_transit(prefix), 'origin_asn': prefix['origin_asn'], 'name': asn_name_query(asn), 'nexthop_ip': prefix['nexthop'], 'nexthop_ip_dns': reverse_dns_query(prefix['nexthop']), 'nexthop_asn': prefix['nexthop_asn'], 'as_path': prefix['as_path'], 'updated': prefix['age'] }) return jsonify({ 'asn': asn, 'name': asn_name_query(asn), 'origin_prefix_count': routes.count(), 'is_peer': is_peer(asn), 'origin_prefix_list': prefixes })
def get_domain(domain): domain = domain.lower() org = domain.split('.')[-2] name_servers = dns_query(domain, 'NS') soa = dns_query(domain, 'SOA') local_ns = '' if org in soa.lower(): local_ns = soa.lower() for ns in name_servers: if org in ns.lower(): local_ns = ns.lower() if local_ns is '': return jsonify({}) else: domain_ip = str(dns_query(local_ns)) ip_data = get_ip_json(domain_ip) asn = ip_data.get('origin_asn') db = myStats.db originated = [] prefixes = db.bgp.find({'origin_asn': asn, 'active': True}) for prefix in prefixes: originated.append(prefix['_id']) return jsonify({ 'domain': domain, 'A Record': dns_query(domain), 'SOA/NS Record': local_ns, 'SOA/NS IP': domain_ip, 'asn': asn, 'name': asn_name_query(asn), 'originated_prefix_count': len(originated), 'originated_prefix_list': originated })
def get_list_of(self, customers=False, peers=False, community=C.CUSTOMER_BGP_COMMUNITY): """Return a list of prefix dictionaries. Specify which type of prefix to return by setting *customers* or *peers* to True.""" if peers: query_results = { prefix['nexthop_asn'] for prefix in self.db.bgp.find({'active': True}) } if customers: query_results = { prefix['nexthop_asn'] for prefix in self.db.bgp.find({ 'communities': community, 'active': True }) } return [ { 'asn': asn if asn is not None else C.DEFAULT_ASN, # Set "None" ASNs to default 'name': asn_name_query(asn), 'ipv4_origin_count': self.db.bgp.find({ 'origin_asn': asn, 'ip_version': 4, 'active': True }).count(), 'ipv6_origin_count': self.db.bgp.find({ 'origin_asn': asn, 'ip_version': 6, 'active': True }).count(), 'ipv4_nexthop_count': self.db.bgp.find({ 'nexthop_asn': asn, 'ip_version': 4, 'active': True }).count(), 'ipv6_nexthop_count': self.db.bgp.find({ 'nexthop_asn': asn, 'ip_version': 6, 'active': True }).count(), 'asn_count': len( self.db.bgp.distinct('as_path.1', { 'nexthop_asn': asn, 'active': True })) } for asn in query_results ]
def bgp_index(): data = myStats.get_data() top_peers = data['top_n_peers'] cidr_breakdown = data['cidr_breakdown'] communities = data['communities'] peers = data['peers'] source_asn = C.DEFAULT_ASN source_asn_name = asn_name_query(C.DEFAULT_ASN) customer_bgp_community = C.CUSTOMER_BGP_COMMUNITY transit_bgp_community = C.TRANSIT_BGP_COMMUNITY peer_bgp_community = C.PEER_BGP_COMMUNITY return render_template('bgp.html', **locals())
def get_nexthop_prefixes(asn): db = myStats.db nexthop = [] prefixes = db.bgp.find({'nexthop_asn': asn, 'active': True}) for prefix in prefixes: nexthop.append(prefix['_id']) return jsonify({ 'asn': asn, 'name': asn_name_query(asn), 'nexthop_prefix_count': len(nexthop), 'nexthop_prefix_list': nexthop })
def get_originated_prefixes(asn): db = myStats.db originated = [] prefixes = db.bgp.find({'origin_asn': asn, 'active': True}) for prefix in prefixes: originated.append(prefix['_id']) return jsonify({ 'asn': asn, 'name': asn_name_query(asn), 'originated_prefix_count': len(originated), 'originated_prefix_list': originated })
def get_downstream_asns(asn): db = myStats.db asn_list = [] large_query = 200 downstream_asns = db.bgp.distinct('as_path.1', { 'nexthop_asn': asn, 'active': True }) for downstream in downstream_asns: if len(downstream_asns) > large_query: dns_name = "(LARGE QUERY - DNS LOOKUP DISABLED)" else: dns_name = asn_name_query(downstream) asn_list.append({'asn': downstream, 'name': dns_name}) sorted_asn_list = sorted(asn_list, key=lambda k: k['asn']) return jsonify({ 'asn': asn, 'name': asn_name_query(asn), 'downstream_asns_count': len(asn_list), 'downstream_asns': sorted_asn_list })
def top_peers(self, count): """Return a sorted list of top peer dictionaries ordered by prefix count. Limit to *count*.""" peers = { peer: self.db.bgp.find({ 'nexthop_asn': peer, 'active': True }).count() for peer in self.db.bgp.distinct('nexthop_asn') } return [{ 'asn': asn[0], 'count': asn[1], 'name': asn_name_query(asn[0]) } for asn in self.take( count, sorted(peers.items(), key=lambda x: x[1], reverse=True))]
def get_transit_prefixes(asn): db = myStats.db all_asns = db.bgp.find({'active': True}) prefixes = [] for prefix in all_asns: if prefix['as_path']: if asn in prefix['as_path']: prefixes.append(prefix['_id']) else: pass else: pass return jsonify({ 'asn': asn, 'name': asn_name_query(asn), 'transit_prefix_count': len(prefixes), 'transit_prefix_list': prefixes })
def get_nexthop_prefixes_version(asn, version): db = myStats.db nexthop = [] v = 4 if version.lower() == 'ipv6': v = 6 prefixes = db.bgp.find({ 'nexthop_asn': asn, 'ip_version': v, 'active': True }) for prefix in prefixes: nexthop.append(prefix['_id']) return jsonify({ 'asn': asn, 'name': asn_name_query(asn), 'nexthop_prefix_count': len(nexthop), 'nexthop_prefix_list': nexthop })
def get_originated_prefixes_version(asn, version): db = myStats.db originated = [] v = 4 if version.lower() == 'ipv6': v = 6 prefixes = db.bgp.find({ 'origin_asn': asn, 'ip_version': v, 'active': True }) for prefix in prefixes: originated.append(prefix['_id']) return jsonify({ 'asn': asn, 'name': asn_name_query(asn), 'originated_prefix_count': len(originated), 'originated_prefix_list': originated })