mirror of
https://github.com/gaoyifan/china-operator-ip.git
synced 2025-07-14 05:12:28 +08:00
refact shell script
* fix wget save to a wrong file name with a same name file * beautify log format * print asn on log * modify/add DEBUG, SKIP_DATA_PREPARATION environment variables
This commit is contained in:
parent
147e983364
commit
ea485fe7e4
34
common.sh
Executable file
34
common.sh
Executable file
@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
[[ $DEBUG == true ]] && set -x
|
||||
|
||||
log_info(){
|
||||
>&2 echo "INFO>" $@
|
||||
}
|
||||
|
||||
get_asn(){
|
||||
local CONF_FILE=$1
|
||||
unset PATTERN
|
||||
unset COUNTRY
|
||||
source $CONF_FILE
|
||||
grep -P "${COUNTRY}\$" asnames.txt |
|
||||
grep -Pi "$PATTERN" |
|
||||
awk '{print $1}' |
|
||||
while read ASN; do
|
||||
ASN=${ASN#AS}
|
||||
if grep '\.' <(echo $ASN) > /dev/null; then
|
||||
ASN_HIGH=${ASN%.*}
|
||||
ASN_LOW=${ASN#*.}
|
||||
ASN=$((ASN_HIGH * 65536 + ASN_LOW))
|
||||
fi
|
||||
echo $ASN
|
||||
done
|
||||
}
|
||||
|
||||
prepare_data(){
|
||||
wget http://bgp.potaroo.net/as1221/asnames.txt -O asnames.txt
|
||||
wget http://archive.routeviews.org/dnszones/rib.bz2 -O rib.bz2
|
||||
log_info "runing bgpdump ..."
|
||||
docker run -it --rm -v `pwd`:/bgpdump -w /bgpdump gaoyifan/bgpdump bgpdump -m -O rib.txt rib.bz2
|
||||
log_info "done"
|
||||
}
|
41
generate.sh
41
generate.sh
@ -1,43 +1,14 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
if [[ -f /etc/kern_loader.conf ]]; then
|
||||
alias grep='ggrep'
|
||||
fi
|
||||
|
||||
get_asn(){
|
||||
CONF_FILE=$1
|
||||
unset PATTERN
|
||||
unset COUNTRY
|
||||
source $CONF_FILE
|
||||
grep -P "${COUNTRY}\$" asnames.txt |
|
||||
grep -Pi "$PATTERN" |
|
||||
awk '{print $1}' |
|
||||
while read ASN; do
|
||||
ASN=${ASN#AS}
|
||||
if grep '\.' <(echo $ASN) > /dev/null; then
|
||||
ASN_HIGH=${ASN%.*}
|
||||
ASN_LOW=${ASN#*.}
|
||||
ASN=$((ASN_HIGH * 65536 + ASN_LOW))
|
||||
fi
|
||||
echo $ASN
|
||||
done
|
||||
}
|
||||
|
||||
prepare_data(){
|
||||
wget http://bgp.potaroo.net/as1221/asnames.txt
|
||||
wget http://archive.routeviews.org/dnszones/rib.bz2
|
||||
>&2 echo "runing bgpdump ..."
|
||||
docker run -it --rm -v `pwd`:/bgpdump -w /bgpdump gaoyifan/bgpdump bgpdump -m -O rib.txt rib.bz2
|
||||
>&2 echo "done"
|
||||
}
|
||||
source common.sh
|
||||
set -e
|
||||
[[ $DEBUG != true ]] && prepare_data
|
||||
[[ $SKIP_DATA_PREPARATION != true ]] && prepare_data
|
||||
mkdir -p result
|
||||
find operator -type f -name '*.conf' |
|
||||
ls operator/*.conf |
|
||||
while read file; do
|
||||
operator=${file%.*}
|
||||
operator=${operator##*/}
|
||||
>&2 echo "generating IP list of $operator ..."
|
||||
get_asn $file | xargs bgptools | docker run -i --rm gaoyifan/cidrmerge > result/$operator.txt
|
||||
>&2 echo "done"
|
||||
log_info "generating IP list of $operator ..."
|
||||
get_asn $file | tee /dev/stderr | xargs bgptools | docker run -i --rm gaoyifan/cidrmerge > result/$operator.txt
|
||||
log_info "done"
|
||||
done
|
||||
|
Loading…
Reference in New Issue
Block a user