Skip to content

Commit

Permalink
_ranges-asn4ip.sh: for IPv4 output ASN prefix range list (not definit…
Browse files Browse the repository at this point in the history
…ive)
  • Loading branch information
paulwratt committed Jan 31, 2022
1 parent 9464fc2 commit 30860b8
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions _ranges-asn4ip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh

if [ "$1" = "" ]; then
echo "List IPv4 ranges assigned to ASN for IP address";
echo "usage: $(basename $0) 192.168.1.12";
exit;
fi;

IP="$(echo "$1" | grep -E -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")";

# make sure IPv4 is at least valid
C=$(echo "$IP" | wc -c);
D=$(echo "$IP" | cut -d \. -f 4);
if [ $C -lt 7 -o -z "$D" ]; then
echo "error: not IPv4: $IP";
exit;
fi;

A="$(curl -s https://freeapi.dnslytics.net/v1/ip2asn/$IP)";

# check if the results are an announced IP address
# and therefore has an ASN
if [ "$(echo "$A" | grep true)" = "" ]; then
echo "announced: false";
exit;
fi;

CIDR="$(echo "$A" | cut -d \" -f 10)";
ASN="$(echo "$A" | cut -d \" -f 13 | grep -E -o "[0-9]{3,6}")";

curl -s "https://www.dan.me.uk/bgplookup?asn=$ASN" | grep '^<tr><td' | grep -v : | cut -d \ -f 4 ;

0 comments on commit 30860b8

Please sign in to comment.