This repository has been archived by the owner on Sep 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hurricaneElectricLookup.py
executable file
·75 lines (55 loc) · 1.8 KB
/
hurricaneElectricLookup.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
70
71
72
73
74
75
#!/usr/bin/env python
"""
-------------------------------------------------------------------------------
Name: hurricaneElectricLookup.py
Purpose: Look up target network ranges using bgp.he.net
Author: Justin Kennedy (@jstnkndy)
-------------------------------------------------------------------------------
"""
from bs4 import BeautifulSoup
import requests
import sys
import re
from netaddr import *
def usage():
print "Usage: %s <search string>" % sys.argv[0]
def main():
if len(sys.argv) != 2:
usage()
sys.exit()
search = sys.argv[1]
headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36"}
url = "http://bgp.he.net/search?search%5Bsearch%5D=" + search + "&commit=Search"
browser = requests.get(url, headers=headers)
soup = BeautifulSoup(browser.text)
table = soup.find("table")
try:
rows = table.findAll("tr")
except:
print "No results found"
sys.exit()
#dictionary to store the cidrs
cidrDict = dict()
for row in rows:
tds = row.findAll("td")
try:
cidr = str(tds[0].get_text())
company = str(tds[1].get_text())
#extract IPv4 CIDRs only
if not re.match(ur'\d+\.\d+\.\d+\.\d+/\d{1,2}',cidr):
continue
except:
continue
cidrDict.setdefault(company,[]).append(cidr)
for key in cidrDict:
print key+":"
#merge the CIDRs to remove any duplicates
s1 = IPSet()
for cidr in cidrDict[key]:
s1.add(cidr)
#print the resulting cidrs
for cidr in s1.iter_cidrs():
print cidr
print ""
if __name__ == "__main__":
main()