Created
July 22, 2024 19:29
-
-
Save defparam/ab1e6b249ceaed51ae60ff6479b67869 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# Copyright (c) 2024 defparam | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
# | |
# usage: ./cidr_info.py <org_name> (i.e. ./cidr_info.py paypal) | |
# | |
# Note: you likely need to pip install BeautifulSoup4 and requests | |
from bs4 import BeautifulSoup | |
import sys | |
import requests | |
def get_cidrs_from_asn(asn): | |
url = f"https://bgp.he.net/{asn}" | |
r = requests.get(url) | |
soup = BeautifulSoup(r.text, "html.parser") | |
# find the table with an id that contains table_prefixes | |
try: | |
table = soup.find_all("table", id=lambda x: x and "table_prefixes" in x)[0] | |
except: | |
return [] | |
# print the first and second column for each row | |
ret = [] | |
for row in table.find_all("tr"): | |
try: | |
ret += [row.find_all("td")[0].text.strip()] | |
except: | |
continue | |
return ret | |
def lookup_asn_from_org(org): | |
url = f"https://bgp.he.net/search?search%5Bsearch%5D={org}&commit=Search" | |
r = requests.get(url) | |
soup = BeautifulSoup(r.text, "html.parser") | |
table = soup.find_all("table")[0] | |
# print the first and second column for each row | |
database = {} | |
for row in table.find_all("tr"): | |
try: | |
if row.find_all("td")[1].text == "ASN": | |
database[row.find_all("td")[0].text] = row.find_all("td")[2].text | |
except: | |
continue | |
return database | |
if __name__ == "__main__": | |
org = sys.argv[1] | |
for k,v in lookup_asn_from_org(org).items(): | |
if org.lower() not in v.lower(): | |
continue | |
print(f"------- {k} - {v}-------", file=sys.stderr, flush=True) | |
for cidr in get_cidrs_from_asn(k): | |
print(cidr, flush=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment