From a45994ec7367b31fcc9881e5c4abb859c7ba8fe5 Mon Sep 17 00:00:00 2001 From: smolkik_adm Date: Mon, 18 May 2026 04:55:04 +0000 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20bgp=5Fprefix.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bgp_prefix.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 bgp_prefix.py diff --git a/bgp_prefix.py b/bgp_prefix.py new file mode 100644 index 0000000..8a146e6 --- /dev/null +++ b/bgp_prefix.py @@ -0,0 +1,49 @@ +#!/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() \ No newline at end of file