-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathget-ripe-ips
executable file
·137 lines (107 loc) · 3.18 KB
/
get-ripe-ips
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
# Get country IP addresses from RIPE
## See https://stat.ripe.net/docs/data_api
## apt install jq
myversion=0.24.2
me=$(basename "$0")
ripe_url="https://stat.ripe.net/data/country-resource-list/data.json?v4_format=prefix&resource="
#### FUNCTIONS
usage() {
cat <<EOF
Usage: $me -c country [more options]
Options:
-c tld : tld/country code
-o output dir : Output directory (default: /tmp)
-s size : minimum file size to trust the data (default = 5000)
-d : Debug
-h : This help
EOF
}
die() {
echo "$@" 1>&2
exit 1
}
warn() {
er="$@"
echo "$er" 1>&2
errors+=("$er")
}
#### PARSE ARGUMENTS
while getopts "c:o:s:dh" opt; do
case $opt in
c) country=$OPTARG;;
o) out_dir=$OPTARG;;
s) min_size=$OPTARG;;
d) debug=true;;
h) usage;;
esac
done
shift $((OPTIND -1))
min_size=${min_size:-5000} # default is 5000 bytes
out_dir=${out_dir:-/tmp}
if [ -z "$country" ]; then
usage
exit 1
fi
url="$ripe_url$country"
# use curl or wget, depending on which one we find
curl_or_wget=$(if hash curl 2>/dev/null; then echo "curl -s"; elif hash wget 2>/dev/null; then echo "wget -qO-"; fi);
if [ -z "$curl_or_wget" ]; then
echo "Neither curl nor wget found. Cannot download data." >&2
exit 1
fi
# check we have jq
if ! hash jq; then
echo "Cannot find the jq Json processor. Install it with 'apt install jq' or similar" >&2
exit 1
fi
tmpfile=$(mktemp "/tmp/ripe-$country-XXXX.json")
[ $debug ] && echo "Trying: $curl_or_wget '$url'" >&2
$curl_or_wget "$url" > $tmpfile
rv=$?
if [ $rv -gt 0 ]; then
echo "Error $rv trying to run $curl_or_wget $url" >&2
exit $rv
fi
status=$(jq -r '.status' $tmpfile)
if [ ! "$status" = "ok" ]; then
ripe_msg=$(jq -r -c '.messages' $tmpfile)
echo "Error: RIPE replied with status = '$status'." >&2
echo "The requested url was '$url'" >&2
echo "and the messages in their reply were: '$ripe_msg'" >&2
echo "The full response is in $tmpfile" >&2
exit 1
fi
ripe_db_time=$(jq -r '.data.query_time' $tmpfile)
[ $debug ] && echo "ripe_db_time=$ripe_db_time" >&2
date_new=$(date -d "$ripe_db_time" +%s)
family=ipv4
out_list="$out_dir/${family}_$country"
if [ -f "$out_list" ]; then
date_old=$(stat --printf "%Y" "$out_list")
else
date_old=0
fi
if [ ! $date_new -gt $date_old ]; then
[ $debug ] && echo "data.querytime $date_new ($ripe_db_time) not newer than $out_list time $date_old ($(date --date=@$date_old)). Exiting" >&2
[ $debug ] && echo "Not removing $tmpfile!" >&2
exit 0
fi
for family in ipv4 ipv6; do
out_list="$out_dir/${family}_$country"
jq -r ".data.resources.$family | .[]" $tmpfile > $out_list.new
touch -d "$ripe_db_time" "$out_list.new"
### Check for minimum size before updating our list
size=$(stat --printf %s "$out_list.new")
if [ "$size" -gt "$min_size" ]; then
[ $debug ] && echo " size = $size" >&2
mv "$out_list.new" "$out_list"
[ $debug ] && echo "OK $out_list" >&2
else
echo "Warning: $size not greater than minimum $min_size. Probably an error." >&2
dt=$(date +%F_%H%M%S)
echo "Moving $out_list.new to $out_list-$dt.bad" >&2
mv "$out_list.new" "$out_list-$dt.bad"
exit 1
fi
done