ip user info
This commit is contained in:
69
ip_analyzer_app/services/parsing.py
Normal file
69
ip_analyzer_app/services/parsing.py
Normal file
@@ -0,0 +1,69 @@
|
||||
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
|
||||
Reference in New Issue
Block a user