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:
Yifan Gao 2017-03-10 21:15:26 +08:00
parent 147e983364
commit ea485fe7e4
3 changed files with 41 additions and 35 deletions

34
common.sh Executable file
View 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"
}

View File

@ -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

View File

@ -1,5 +1,6 @@
#!/usr/bin/env bash
source common.sh
cd result
ls *.txt |
while read file; do