Created
April 3, 2025 21:44
-
-
Save edsu/eb09297201185e90dcee87b753cae823 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import fileinput | |
import re | |
import subprocess | |
from functools import cache | |
@cache | |
def ips(hostname): | |
for line in subprocess.run( | |
["host", hostname], capture_output=True, check=True, encoding="utf8" | |
).stdout.splitlines(): | |
if m := re.search(r"has address (.+)$", line): | |
yield m.group(1) | |
@cache | |
def whois(ip_address): | |
txt = subprocess.run( | |
["whois", ip_address], capture_output=True, check=True, encoding="utf8" | |
).stdout | |
if m := re.search("^OrgName: +(.+)$", txt, re.MULTILINE): | |
return m.group(1) | |
return None | |
print("hostname,ip,provider") | |
for hostname in fileinput.input(): | |
hostname = hostname.strip() | |
for ip in ips(hostname): | |
print(f'{hostname},{ip},"{whois(ip)}"') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment