forked from gaoyifan/china-operator-ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.sh
executable file
·55 lines (50 loc) · 1.2 KB
/
common.sh
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
#!/usr/bin/env bash
[[ $DEBUG == true ]] && set -x
log_info(){
>&2 echo "INFO>" $@
}
get_asn(){
local CONF_FILE=$1
unset PATTERN
unset COUNTRY
unset EXCLUDE
source $CONF_FILE
EXCLUDE=${EXCLUDE:-"^$"}
grep -P "${COUNTRY}\$" asnames.txt |
grep -Pi "$PATTERN" |
grep -vPi "$EXCLUDE" |
awk '{gsub(/AS/, ""); print $1 }'
}
prepare_data_v4(){
curl -sSLo rib.bz2 http://archive.routeviews.org/dnszones/rib.bz2
log_info "runing bgpdump v4 ..."
bgpdump -m -O rib.txt rib.bz2
log_info "finish bgpdump v4"
}
prepare_data_v6(){
IP6UPSTREAM="http://archive.routeviews.org/route-views6/bgpdata"
MONTH6=$(date -u +%Y.%m)
LATEST6=$(lftp -e 'cls -1;exit' $IP6UPSTREAM/$MONTH6/RIBS/ 2>/dev/null | sort | tail -n 1)
curl -sSLo rib6.bz2 "$IP6UPSTREAM/$MONTH6/RIBS/$LATEST6"
log_info "runing bgpdump v6 ..."
bgpdump -m -O rib6.txt rib6.bz2
log_info "finish bgpdump v6"
}
prepare_data(){
curl -sSL https://bgp.potaroo.net/cidr/autnums.html | awk '-F[<>]' '{print $3,$5}' | grep '^AS' > asnames.txt &
prepare_data_v4 &
prepare_data_v6 &
wait_exit
}
wait_exit(){
local oldstate=$(set +o)
set +e
local s=0
while [[ $s -ne 127 ]]; do
[[ $s -ne 0 ]] && exit $s
wait -n
s=$?
done
eval "$oldstate"
return 0
}