netblock/collect.sh

35 lines
873 B
Bash
Executable File

#!/bin/sh
IPV4_LIST=./ips-v4
IPV6_LIST=./ips-v6
ASLIST=""
# Truncate
true > "$IPV4_LIST"
true > "$IPV6_LIST"
ASLIST="$ASLIST 32934" # Facebook (Meta)
ASLIST="$ASLIST 16509" # Amazon (AWS)
ASLIST="$ASLIST 15169" # Google
ASLIST="$ASLIST 136907" # Huawei Cloud
for ASN in $ASLIST; do
LIST="$(\
curl -L \
'https://stat.ripe.net/data/announced-prefixes/data.json?resource=AS'"$ASN" | \
jq -r '.data.prefixes[].prefix')"
# Cheap trick to separate IPs
echo "$LIST" | grep '\.' >> "$IPV4_LIST"
echo "$LIST" | grep -v '\.' >> "$IPV6_LIST"
done
# Sort from least specific to most
TMPFILE="$(mktemp -p /tmp)"
< "$IPV4_LIST" sort | uniq | sed 's;/;,;g' | xsv sort -s 2 -n | sed 's;,;/;g' > "$TMPFILE"
cp "$TMPFILE" "$IPV4_LIST"
< "$IPV6_LIST" sort | uniq | sed 's;/;,;g' | xsv sort -s 2 -n | sed 's;,;/;g' > "$TMPFILE"
cp "$TMPFILE" "$IPV6_LIST"
rm -v "$TMPFILE"