Skip to content

Instantly share code, notes, and snippets.

@ziot
Created July 15, 2019 23:07
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ziot/090696d9c7fb303bc81cbc0dd6370169 to your computer and use it in GitHub Desktop.
Save ziot/090696d9c7fb303bc81cbc0dd6370169 to your computer and use it in GitHub Desktop.
Get hosts by ASN->CIDR->Hosts via company name
import requests, json
from requests.packages.urllib3.exceptions import InsecureRequestWarning, InsecurePlatformWarning, SNIMissingWarning
from bs4 import BeautifulSoup
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
requests.packages.urllib3.disable_warnings(InsecurePlatformWarning)
requests.packages.urllib3.disable_warnings(SNIMissingWarning)
# another source of cidrs by asn
def getIPCidrs(asn):
url = "https://ipinfo.io/AS{0}".format(asn)
r = requests.get(url, verify=False)
return
def getASNumbers(name):
cidrs = []
asns = []
url = "https://mxtoolbox.com/Public/Lookup.aspx/DoLookup2".format(name)
data = {
"inputText": "asn:{0}".format(name),
"resultIndex": 1
}
r = requests.post(url, json=data, verify=False)
soup = BeautifulSoup((json.loads(r.json()["d"])["HTML_Value"]), 'html.parser')
for td in soup.find_all('td', {"class":"table-column-CIDR_Range"}):
cidrs.append(td.get_text())
for td in soup.find_all('td', {"class":"table-column-As_Number"}):
asns.append(td.get_text())
return cidrs,asns
def dedupe(crtList):
newlist = []
for i in crtList:
if i not in newlist:
newlist.append(i)
return newlist
def getHostsByCidr(cidr):
url = "https://www.robtex.com/cidr/{0}".format(cidr)
r = requests.get(url, verify=False)
soup = BeautifulSoup(r.text, 'html.parser')
hosts = []
for a in soup.find_all('a', href=True):
if "https://www.robtex.com/dns-lookup/" in a['href']:
host = a['href'].replace('https://www.robtex.com/dns-lookup/','')
hosts.append(host)
hosts = dedupe(hosts)
return hosts
input = "wal-mart stores, inc."
cidrs, asns = getASNumbers(input)
hosts = []
for cidr in cidrs:
hosts+=getHostsByCidr(cidr)
hosts = dedupe(hosts)
print(hosts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment