70 lines
2.3 KiB
Python
70 lines
2.3 KiB
Python
import ipaddress
|
|
import re
|
|
|
|
|
|
def parse_ip_list(text: str) -> list[str]:
|
|
"""Parse unique IPv4 addresses from free-form text."""
|
|
normalized = re.sub(r'[,;|\t]+', '\n', text or '')
|
|
lines = normalized.strip().split('\n')
|
|
|
|
ips: list[str] = []
|
|
for line in lines:
|
|
ips.extend(re.findall(r'\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b', line))
|
|
|
|
valid_ips: list[str] = []
|
|
for ip in ips:
|
|
try:
|
|
ipaddress.IPv4Address(ip)
|
|
valid_ips.append(ip)
|
|
except Exception:
|
|
pass
|
|
|
|
return sorted(set(valid_ips), key=valid_ips.index)
|
|
|
|
|
|
def extract_field(line: str) -> str:
|
|
return line.split(':', 1)[1].strip() if ':' in line else ''
|
|
|
|
|
|
def parse_whois(whois_output: str) -> dict[str, str]:
|
|
"""Extract relevant information from WHOIS output, including user/customer labels."""
|
|
info = {
|
|
'org': 'Unknown',
|
|
'user': 'Unknown',
|
|
'country': 'Unknown',
|
|
'netname': 'Unknown',
|
|
'asn': 'Unknown',
|
|
'cidr': 'Unknown',
|
|
}
|
|
|
|
for raw_line in whois_output.split('\n'):
|
|
line = raw_line.strip()
|
|
lowered = line.lower()
|
|
|
|
if line.startswith('Organization:') or line.startswith('org-name:') or line.startswith('OrgName:'):
|
|
value = extract_field(line)
|
|
if value:
|
|
info['org'] = value
|
|
elif line.startswith('Country:') or line.startswith('country:'):
|
|
value = extract_field(line)
|
|
if value:
|
|
info['country'] = value
|
|
elif line.startswith('NetName:') or line.startswith('netname:'):
|
|
value = extract_field(line)
|
|
if value:
|
|
info['netname'] = value
|
|
elif line.startswith('CIDR:') or line.startswith('inetnum:') or line.startswith('route:'):
|
|
value = extract_field(line)
|
|
if value:
|
|
info['cidr'] = value
|
|
elif line.startswith('OriginAS:') or line.startswith('origin:') or line.startswith('originas:'):
|
|
asn = re.search(r'AS\d+', line, re.IGNORECASE)
|
|
if asn:
|
|
info['asn'] = asn.group().upper()
|
|
elif re.match(r'^(user|customer|owner|descr):', lowered):
|
|
value = extract_field(line)
|
|
if value and info['user'] == 'Unknown':
|
|
info['user'] = value
|
|
|
|
return info
|