Skip to content

Instantly share code, notes, and snippets.

@holly
Created August 12, 2013 13:37
Show Gist options
  • Save holly/6210864 to your computer and use it in GitHub Desktop.
Save holly/6210864 to your computer and use it in GitHub Desktop.
ipv4-for-delegated-apnic-latest.sh CC [DELEGATED_APNIC_LATEST]
#!/bin/bash
MASK=32
SEP='|'
IPV4=ipv4
SCRIPT=$(basename $0)
DELEGATED_APNIC_LATEST=http://ftp.apnic.net/stats/apnic/delegated-apnic-latest
if [ $# -lt 1 ]; then
echo "Usage: $SCRIPT CC [DELEGATED_APNIC_LATEST]"
exit 1
fi
cc=$1
list=$2
if [ -z "$list" ]; then
list=/tmp/${SCRIPT}-$$
curl -s -L $DELEGATED_APNIC_LATEST >$list
fi
grep $cc $list | grep $IPV4 | while read line; do
results=($(echo $line | awk -F$SEP '{ printf "%s %s\n", $4, $5 }'))
square=0
while [ ${results[1]} != 1 ]; do
results[1]=$((${results[1]} / 2))
square=$(($square + 1))
done
subnet=$(($MASK - $square))
echo "${results[0]}/$subnet"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment