#!/usr/bin/env python3 import sys import re import urllib.request import urllib.parse def fetch_prefixes(keyword: str) -> list[str]: url = ( "https://bgp.he.net/search?" + urllib.parse.urlencode({"search[search]": keyword, "commit": "Search"}) ) req = urllib.request.Request( url, headers={"User-Agent": "Mozilla/5.0 (compatible; bgp-search/1.0)"}, ) with urllib.request.urlopen(req, timeout=15) as resp: html = resp.read().decode("utf-8", errors="replace") prefixes = re.findall( r'href="/net/([^"]+)"', html, ) return sorted(set(prefixes)) def main(): if len(sys.argv) < 2: print("Use: python3 bgp_search.py ", file=sys.stderr) print("For example: python3 bgp_search.py ozon", file=sys.stderr) sys.exit(1) keyword = sys.argv[1] prefixes = fetch_prefixes(keyword) if not prefixes: print(f"Network for '{keyword}' not found.", file=sys.stderr) sys.exit(0) for prefix in prefixes: print(prefix) if __name__ == "__main__": main()