Skip to content

Instantly share code, notes, and snippets.

@kacchan822
Last active March 3, 2016 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kacchan822/3120743d6050f2e04666 to your computer and use it in GitHub Desktop.
Save kacchan822/3120743d6050f2e04666 to your computer and use it in GitHub Desktop.
simple_whois.py
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
#
import sys
import ipaddress
from ipwhois import IPWhois
def swhois(ip):
obj = IPWhois(ip)
results = obj.lookup(get_referral=True)
cc = results['asn_country_code']
cidrs = []
for result_net in results['nets']:
cidr = result_net['cidr'].split('/')[1]
cidrs.append(int(cidr))
ipwithcidr = str(ipaddress.IPv4Network(ip+'/'+str(max(cidrs)),False))
return '{0} --> {1} {2}'.format(ip,ipwithcidr,cc)
if __name__ == '__main__':
print(swhois(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment