Skip to content

Instantly share code, notes, and snippets.

@ssnkhan
Created October 11, 2022 22:58
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 ssnkhan/0629125c76ab821513c39d2cbe2b37d9 to your computer and use it in GitHub Desktop.
Save ssnkhan/0629125c76ab821513c39d2cbe2b37d9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Uses Shadowserver's `ASN and Network Queries` API
# See https://www.shadowserver.org/what-we-do/network-reporting/api-asn-and-network-queries/
# Preferred over similar free services as it offers better normalisation of ASN Names
# Beware of rate limiting, 10 queries/second
import json
import requests
def get_whois(ip):
url = "https://api.shadowserver.org/net/asn?origin=" + ip
response = requests.get(url).json()
asn = int(response[0].get("asn"))
asn_name = response[0].get("asname_long").rstrip(".")
asn_short = response[0].get("asname_short")
bgp_prefix = response[0].get("prefix")
country = response[0].get("geo")
return asn, asn_name, asn_short, bgp_prefix, country
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment