Skip to content

Instantly share code, notes, and snippets.

@goodarzi
Created October 30, 2023 20:07
Show Gist options
  • Save goodarzi/c06bac99ff5b6548d14fe17a725a92ec to your computer and use it in GitHub Desktop.
Save goodarzi/c06bac99ff5b6548d14fe17a725a92ec to your computer and use it in GitHub Desktop.
Get ASN from ip address and then create ipset with all IP4/6 prefix associated with ASN
#!/usr/bin/env bash
[[ -v 1 ]] && IP=$1 || { echo "Please enter ip address"; exit 1; }
ASNS=$(curl -s https://stat.ripe.net/data/network-info/data.json?resource=$IP | jq -r .data.asns[])
for asn in $ASNS; do
echo "-i origin AS${asn}" | nc whois.radb.net 43 | tee >(grep '^route6:' | sed 's/.*\s\([abcdef0-9:\/]*\)/\1/' >> as${asn}_route6) >(grep '^route:' | sed 's/.*\s\([-0-9\.\/]*\)/\1/' >> as${asn}_route4) 1>/dev/null
done
ipset create as${asn}_route4 nethash -exist
ipset create as${asn}_route6 nethash family inet6 -exist
while read ip;do ipset add as${asn}_route4 $ip -exist;done < ./as${asn}_route4
while read ip;do ipset add as${asn}_route6 $ip -exist;done < ./as${asn}_route6
ipset list -t as${asn}_route6
ipset list -t as${asn}_route4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment