Get all assigned IP for country from Regional Internet Registry

Simple script which will fetch latest delegated IP, filter it by country (this script only returned IP registered to Malaysian entity), and return the complete address in CIDR. Tested only with APNIC, so might need modification for use with other registry.

#!/bin/bash

TMP=/tmp
FILE=delegated-apnic-extended-latest
URL="ftp://ftp.apnic.net/pub/stats/apnic/"
COUNTRY=MY
cd $tmp
rm -rf $FILE
wget $URL$FILE

# coversion array
prefix=("16:65536" "24:256" "17:32768" "18:16384" "19:8192" "20:4096" "21:2048" "22:1024" "23:512" "14:262144" "15:131072" "13:524288" "12:1048576" "12:2097152")

for i in `cat $FILE |grep ipv4|grep $COUNTRY`
do 

        for j in ${prefix[@]}
        do
                KEY=${j%%:*}
                VALUE=${j##*:}
                i=${i/$VALUE/$KEY}
        done
        echo $i|awk -F'|' '{print $4"/"$5;}'
done