This commit is contained in:
Be
2024-03-03 12:45:49 +01:00
parent 27407898fc
commit 69655d18d3
4 changed files with 32 additions and 55 deletions

View File

@@ -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:

View File

@@ -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}")

Binary file not shown.

28
pylib/whois.py Executable file
View File

@@ -0,0 +1,28 @@
import socket
def whois_query(query, get_field="netname"):
whois_server = "whois.ripe.net"
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