From 69655d18d38f8857de7b27bff59011e6127dacee Mon Sep 17 00:00:00 2001 From: Be Date: Sun, 3 Mar 2024 12:45:49 +0100 Subject: [PATCH] whois --- get_info_from_ripe.py | 29 ++--------------------- network_list_from_netname.py | 30 ++---------------------- pylib/__pycache__/whois.cpython-311.pyc | Bin 0 -> 1436 bytes pylib/whois.py | 28 ++++++++++++++++++++++ 4 files changed, 32 insertions(+), 55 deletions(-) create mode 100644 pylib/__pycache__/whois.cpython-311.pyc create mode 100755 pylib/whois.py diff --git a/get_info_from_ripe.py b/get_info_from_ripe.py index a360616..111ee57 100755 --- a/get_info_from_ripe.py +++ b/get_info_from_ripe.py @@ -2,34 +2,9 @@ import argparse import requests -import socket +from pylib.whois import whois_query url = "https://stat.ripe.net/data/country-resource-list/data.json?resource=RU&v4_format=prefix" -whois_server = "whois.ripe.net" - -def whois_query(whois_server, query, get_field="netname"): - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect((whois_server, 43)) - - query = f"{query}\r\n" - s.send(query.encode()) - - response = '' - while True: - data = s.recv(4096) - try: - response += data.decode('utf-8') - except: - response += data.decode('latin-1') - if not data: - break - s.close() - - for line in response.split('\n'): - if line.startswith(get_field + ':'): - return line.strip() - - return None def get_data(json, file, attr, field, prefix=""): limit = args.limit @@ -41,7 +16,7 @@ def get_data(json, file, attr, field, prefix=""): if count > limit: response = None else: - response = whois_query(whois_server, x, field) + response = whois_query(x, field) if response is None: name = "-no-description-" else: diff --git a/network_list_from_netname.py b/network_list_from_netname.py index db42e95..0e4e7f4 100755 --- a/network_list_from_netname.py +++ b/network_list_from_netname.py @@ -1,36 +1,10 @@ #!/usr/bin/env python3 -import socket import argparse import requests import ipaddress import re - -whois_server = "whois.ripe.net" - -def whois_query(whois_server, query, get_field="netname"): - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect((whois_server, 43)) - - query = f"{query}\r\n" - s.send(query.encode()) - - response = '' - while True: - data = s.recv(4096) - try: - response += data.decode('utf-8') - except: - response += data.decode('latin-1') - if not data: - break - s.close() - - for line in response.split('\n'): - if line.startswith(get_field + ':'): - return line.strip() - - return None +from pylib.whois import whois_query def convert_to_raw_github_url(url): return url.replace("https://github.com/", "https://raw.githubusercontent.com/").replace("/blob", "") @@ -55,7 +29,7 @@ def extract_netname(filename_or_url): for line in lines: if re.match(r'^netname:', line): netname = line.split(':')[1].strip() - response = whois_query(whois_server, netname, "inetnum") + response = whois_query(netname, "inetnum") if response is not None: if not args.quiet: print(f"# Network name: {netname}") diff --git a/pylib/__pycache__/whois.cpython-311.pyc b/pylib/__pycache__/whois.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..398693fd266df7fddb1352da3c6dce2c8785f29d GIT binary patch literal 1436 zcmZ`(&1)M+6rb6h)%RE8kgBG|GBE{VM{1yj*r;}$#vh`t5-j(SKoN>&Kf+PB;XQLYPknU z4y5j3b~T7ljVNQ+5X}GfF3=d!?3lpzJ{E z9bV@)`s%})%XGDR2g#xR2TV+TRlvhN+Ew8D-{jiy@Dv3BqbzFPpy1>FOf|ImP?hzB zCYM|V54DOy^%%w96 zYuul`^V#RQ?Aq$W{1@C^P!)w03{G^WQ0`(%L8XinjTI`~MOiPB3MEx%T++)W+2B6i z$ZLkaDH{(VWxzynHY{i& zD@?bKL9B0QcavWNT~Dedww}BVoV%J_$s244_ls&lU&$u#BRKxSeuM@$oLT%KMr4MWbCKDU^5oC7aJe%rgl=#XPdKaGTI@dM`ZLk ze5t-{32y0fHyqim>{Py^&&{UUAeJC{lik3Tee$dSCx2_H9i8e#r`mz3#&To%)DrMu zHQkOTI?+Tskg%&zKRxY+M=XTBm%D*WRv6^12$EdSmbRC^O+QW7)5nqMUg$+=-)KiB zI*|zr2~w=*8l>wF@6PSa?N7J#ALrWs$&P>0R*H2;-r1XZG4p-uW$Gv}*8f-{6f74C zL>eFI0obiqp6IP{^lE(c)+b^dYxV}l1