Skip to content

Instantly share code, notes, and snippets.

@edsu
Created April 3, 2025 21:44
Show Gist options
  • Save edsu/eb09297201185e90dcee87b753cae823 to your computer and use it in GitHub Desktop.
Save edsu/eb09297201185e90dcee87b753cae823 to your computer and use it in GitHub Desktop.
#!/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