-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
64 lines (49 loc) · 2.29 KB
/
main.py
File metadata and controls
64 lines (49 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
"""
Example of me using multiple functions to output helpful Data
Functions for code are located in bgpview_api.property
"""
import sys
import ipaddress
import bgpview_api
def ipaddress_check():
"""Using ipaddress library to validate a IPv4/IPv6 is being provided"""
input_ip = input('Please provide IPv4 or IPv6 Public IP address to run search against: ')
try:
ip_check = ipaddress.ip_address(input_ip)
print('%s is a correct IP%s address.' % (ip_check, ip_check.version))
print('Proceeding with gathering API Data\n')
return input_ip
except ValueError:
print('\nAddress is invalid: %s' % input_ip)
print('Exiting code, please retry with a valid IPv4 or IPv6 address')
sys.exit()
def main():
"""Simple example script leveraging return data from API calls"""
ip_data = ipaddress_check()
ip_lookup = bgpview_api.bgp_ip_lookup(ip_data)
if ip_lookup['status'] == 'ok':
print(ip_lookup['status_message'])
#Gather how many prefix routes are seen over BGP for IP
count_bgp_routes = len(ip_lookup['data']['prefixes'])
print('There is '+ str(count_bgp_routes) + ' BGP routes/paths present for ' + str(ip_data))
print('\n')
#Loop through route/prefix and find ISP for each
#Pending better iteration rebuild
for i in range(count_bgp_routes):
cidr = ip_lookup['data']['prefixes'][i]['prefix']
asn = ip_lookup['data']['prefixes'][i]['asn']['asn']
print(cidr + ' is managed by ASN#: ' + str(asn))
#Run function using ASN # to find ISPs this client/Public prefix uses
asn_peers = bgpview_api.bgp_asn_upstreams(asn)
#Will need to consider rebuilding for IPv6 logic versus IPv4 logic
isp_peers = len(asn_peers['data']['ipv4_upstreams'])
print('Which is peered with the following ' + str(isp_peers) + ' ISP providers\n')
for peer in range(isp_peers):
isp = asn_peers['data']['ipv4_upstreams'][peer]['name']
isp_asnum = asn_peers['data']['ipv4_upstreams'][peer]['asn']
print(str(isp) + ' AS#: ' + str(isp_asnum))
else:
print('No BGP Route for provided IP address')
if __name__ == '__main__':
main()