ASN Details

Code is community submitted, community supported, and recognized as ‘Use At Your Own Risk’.

Short Description

This script adds a cli tool (in python) to quickly look up autonomous systems by number or name.

Problem solved by this Code Snippet

Not a problem per se, but makes information retrieval easier by not requiring a browser session looking up information.

How to use this Code Snippet

(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

Code Snippet Meta Information

  1. Version: 0.1
  2. Coding Language: python

Full Code Snippet

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)
Published Jun 08, 2022
Version 1.0
No CommentsBe the first to comment