Skip to content

Instantly share code, notes, and snippets.

@kgersen
Created August 9, 2018 13:26
Show Gist options
  • Save kgersen/024d6dfb49c328545342dde92aaf354e to your computer and use it in GitHub Desktop.
Save kgersen/024d6dfb49c328545342dde92aaf354e to your computer and use it in GitHub Desktop.
RIPE alloclist to database conversion
#!/bin/bash
# convert RIPE allocation list to database friendly format
# (c) kgersen 2018 for https://lafibre.info
# tab (\t) is used as separator in output
# output
# country ispcode ispname date 4|6 block block_size
# optionnal argument: country code to match
url="https://ftp.ripe.net/ripe/stats/membership/alloclist.txt"
alloc2db()
{
skip="no"
while read line; do
case $line in
([a-z]*.[a-z0-9]*)
isp=$line
country=${isp%%.*}
if [ ! -z "$1" ]; then if [ ! "$country" == "$1" ]; then skip=""; continue; fi; fi
skip="no"
ispcode=${isp#*.}
read line
ispname=$line
ispname="${ispname//\"}";;
("")
;;
(*)
if [ -z "$skip" ]; then continue; fi
read -r date block trash <<<"$line"
#date="$(date --date="$date" "+%Y/%m/%d")"
date=${date:0:4}/${date:4:2}/${date:6:2}
size=${block#*/}
if [[ $block = *"::"* ]]; then
ipv="6"
block_size=$[2**(64-$size)]
else
ipv="4"
block_size=$[2**(32-$size)]
fi
echo -e $country$'\t'$ispcode$'\t'$ispname$'\t'$date$'\t'$ipv$'\t'$block$'\t'$block_size;;
esac
done
}
curl -s "$url" | alloc2db $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment