-
Notifications
You must be signed in to change notification settings - Fork 0
/
bgpasn.py
executable file
·69 lines (54 loc) · 1.74 KB
/
bgpasn.py
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
65
66
67
68
69
#!/bin/python3
import argparse
import sys
import subprocess
import requests
from bs4 import BeautifulSoup
URL = "https://bgp.he.net/"
def copy_to_clip(cidr):
process = subprocess.Popen(['xsel', '-bi'], stdin=subprocess.PIPE)
process.communicate(input="\n".join(cidr).encode('utf-8'))
def get_ips(ASN, v6=False):
ASN_URL = URL + ASN.strip()
page = requests.get(ASN_URL)
soup = BeautifulSoup(page.content, "lxml")
ranges = []
prefixes = soup.find("div", id="prefixes")
if prefixes:
prefixes_table = prefixes.find("table", id="table_prefixes4")
for cidr in prefixes_table.tbody.find_all("a"):
ranges.append(cidr.text)
print(cidr.text)
if v6:
prefixes6 = soup.find("div", id="prefixes6")
if prefixes6:
prefixes_table = prefixes6.find("table", id="table_prefixes6")
for cidr in prefixes_table.tbody.find_all("a"):
ranges.append(cidr.text)
print(cidr.text)
return ranges
def main():
cidrs = []
parser = argparse.ArgumentParser(
description="ASN to IP range scraper from bgp.he.net"
)
parser.add_argument("-a", type=str, help="ASN")
parser.add_argument("-v6", action="store_true", help="Include ipv6 ranges in output")
args = parser.parse_args()
if args.a:
try:
cidrs = get_ips(args.a, args.v6)
copy_to_clip(cidrs)
except:
print("Error")
elif sys.stdin.isatty():
parser.print_help()
else:
for line in sys.stdin:
try:
cidrs = get_ips(line.strip())
copy_to_clip(cidrs)
except:
print("Error")
if __name__ == "__main__":
main()