Skip to content

Instantly share code, notes, and snippets.

@yi-jiayu
Last active November 6, 2023 09:49
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yi-jiayu/0fcc8fda6b6311a537e17539c7468441 to your computer and use it in GitHub Desktop.
Save yi-jiayu/0fcc8fda6b6311a537e17539c7468441 to your computer and use it in GitHub Desktop.
asn() {
if [ -z "$1" ]
then
echo "usage: asn url" 1>&2
return 1
fi
ips=$(dig $DIG_ARGS +short $1)
if [ -z "$ips" ]
then
echo "dig $1 failed" 1>&2
return 1
fi
# convert ips to array
iparr=($ips)
# if there is more than one ip address,
# use the bulk netcat interface
# else use the whois interface
if (( ${#iparr[*]} > 1 ))
then
printf "begin\nverbose\n$ips\nend\n" | netcat whois.cymru.com 43
else
whois -h whois.cymru.com " -v $ips"
fi
}

URL to ASN lookup

Usage

$ source asn.sh
$ asn github.com
Bulk mode; whois.cymru.com [2018-09-29 04:12:30 +0000]
36459   | 192.30.253.113   | 192.30.253.0/24     | US | arin     | 2012-11-15 | GITHUB - GitHub, Inc., US
36459   | 192.30.253.112   | 192.30.253.0/24     | US | arin     | 2012-11-15 | GITHUB - GitHub, Inc., US
$ asn google.com
AS      | IP               | BGP Prefix          | CC | Registry | Allocated  | AS Name
15169   | 216.58.199.142   | 216.58.199.0/24     | US | arin     | 2012-01-27 | GOOGLE - Google LLC, US
$ 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment