· 6 years ago · Oct 08, 2019, 08:20 AM
1if PGPASSWORD=test psql -U postgres -w -lqt | cut -d \| -f 1 | grep -qw asn_lookup;then
2 echo "ASN Database already exists, only updating!"
3 cd /tmp/
4 curl -O -s https://iptoasn.com/data/ip2asn-combined.tsv.gz
5 gunzip ip2asn-combined.tsv.gz
6 echo "DROP table asn; CREATE table asn (range_start INET,range_end INET, AS_number VARCHAR(10) ,country_code VARCHAR(7),AS_description VARCHAR(250)); COPY asn FROM '/tmp/ip2asn-combined.tsv' DELIMITER E'\t';" | PGPASSWORD=test psql -U postgres -w asn_lookup
7 rm ip2asn-combined.tsv
8else
9 echo "ASN Database does not exist, creating it!"
10 echo "CREATE DATABASE ASN_lookup OWNER postgres;" |PGPASSWORD=test psql -U postgres -w
11 cd /tmp/
12 curl -O -s https://iptoasn.com/data/ip2asn-combined.tsv.gz
13 gunzip ip2asn-combined.tsv.gz
14 echo "CREATE table asn (range_start INET,range_end INET, AS_number VARCHAR(10) ,country_code VARCHAR(7),AS_description VARCHAR(250)); COPY asn FROM '/tmp/ip2asn-combined.tsv' DELIMITER E'\t';" | PGPASSWORD=test psql -U postgres -w asn_lookup
15fi
16echo "Done with ASN Database!"