summaryrefslogtreecommitdiff
path: root/whois.py
blob: be980344ae4f46b2ba8d4e65555c48d9cd974798 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from subprocess import run

def extract_field(field, response):
    for line in response:
        if line.startswith(field):
            return line.split('    ')[-1].strip()
    return 'Unknown'
    
def whois(ip):
    cp = run(['whois', ip], capture_output=True)
    lines = cp.stdout.decode().split('\n')
    fields = ['route', 'origin', 'descr']
    res = {f: extract_field(f, lines) for f in fields}
    return res