Skip to content

Instantly share code, notes, and snippets.

@jasonrahm
Created June 8, 2022 20:21
Show Gist options
  • Save jasonrahm/d448f1ca06ee01d9b4a289d7bc2770c2 to your computer and use it in GitHub Desktop.
Save jasonrahm/d448f1ca06ee01d9b4a289d7bc2770c2 to your computer and use it in GitHub Desktop.
BGP ASN Info - credit to showipintbri
import bgpstuff
import argparse
import re
def get_as_name(client, as_number):
client.get_as_name(as_number)
if client.status_code == 200 and client.exists:
print(f"{as_number} = {c.as_name}")
def get_as_number(client, as_name):
client.get_as_names()
all_names = client.all_as_names
re_string = f".*{as_name}.*"
re_params = re.compile(re_string, re.IGNORECASE)
for i in sorted(all_names):
name = all_names[i]
try:
m = re_params.match(name)
if m is not None:
result = str(m.group())
print(f"{i} = {result}")
except:
pass
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Lookup ASN Name by ASN Number.")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"-a",
"--as_name",
metavar="ASN_NAME",
type=str,
action="store",
help="The string you want contained in the ASN Name.",
)
group.add_argument(
"-n",
"--as_num",
metavar="ASN",
type=int,
action="store",
help="The ASN Number you want to lookup.",
)
args = parser.parse_args()
c = bgpstuff.Client()
if args.as_num:
get_as_name(c, args.as_num)
elif args.as_name:
get_as_number(c, args.as_name)
@f5-rahm
Copy link

f5-rahm commented Jun 8, 2022

(venv) me@macos scripts % python asn_info.py -a f5        
22317 = F5-NETWORKS
209077 = NEWTON-AS D8C2 59FB C6DE 0B2B D56B EAF9 3CF5 EA56 759A E33D
268951 = F5 INFORMATICA COMERCIO DE PRODUTOS ELETRONICOS LT
398349 = F5-SILVERLINE-LABS
(venv) me@macos scripts % python asn_info.py -n 22317
22317 = F5-NETWORKS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment