def whois_lookup(ip_address): nets = [] err = None try: whois = ipwhois.IPWhois(ip_address).lookup_whois(retry_count=0) countries = ipwhois.utils.get_countries() nets = whois['nets'] for net in nets: net['country'] = countries.get(net['country']) if net['postal_code']: net['postal_code'] = net['postal_code'].replace('-', ' ') except ValueError as e: err = 'Invalid IP address provided: %s.' % ip_address except ipwhois.exceptions.IPDefinedError as e: err = '%s' % e except ipwhois.exceptions.ASNRegistryError as e: err = '%s' % e except Exception as e: err = 'Error: %s' % e host = '' try: host = ipwhois.Net(ip_address).get_host(retry_count=0)[0] except Exception as e: host = 'Not available' whois_info = {"host": host, "nets": nets} if err: whois_info['error'] = err return whois_info
def getASNdata(hostname1): asndata = {'asn':" ",'asn_description':"",'asn_cidr':""} try: netobj = ipwhois.Net(hostname1) netasn = ipwhois.asn.IPASN(netobj) netlookup = netasn.lookup(netasn) asndata['asn'] = netlookup['asn'] asndata['asn_description'] = netlookup['asn_description'] asndata['asn_cidr'] = netlookup['asn_cidr'] return asndata except: asndata['asn']= "lookup fail" return asndata