Skip to content

Commit 59fec29

Browse files
Merge pull request #1313 from lainedfles/ipasn_db_update_script
feat: Add script to automatically update ipasn and asnames databases
2 parents feafe87 + efd8706 commit 59fec29

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

utils/scripts/ipasn_db_update.sh

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Update ipasn and asnames data for use with OpenSnitch
4+
#
5+
# Author: Self Denial <selfdenial at pm dot me>
6+
#
7+
# This script requires the pyasn module from: https://github.com/hadiasghari/pyasn
8+
# Specifically, the pyasn-utils pyasn_util_asnames.py, pyasn_util_download.py,
9+
# and pyasn_util_convert.py. These must be available with the PATH variable.
10+
#
11+
# Example crontab:
12+
#
13+
# Update every 14 days
14+
# 0 0 */14 * * /home/user/.config/opensnitch/ipasn_db_update.sh 2>&1 | logger -t ipasn_db_update.sh
15+
16+
# Vars
17+
OPENSNITCH_CONF_PATH=~/.config/opensnitch
18+
IPASN_FILE="${OPENSNITCH_CONF_PATH}/ipasn_db.dat"
19+
ASNAMES_FILE="${OPENSNITCH_CONF_PATH}/asnames.json"
20+
RIBDATA_FILE="${OPENSNITCH_CONF_PATH}/rib-data.bz2"
21+
22+
# Ensure pyasn-utils are available
23+
for PYASN_UTIL in pyasn_util_{asnames,convert,download}.py; do
24+
if ! command -v "$PYASN_UTIL" &>/dev/null; then
25+
echo "$PYASN_UTIL not found! Please ensure that the pyasn-utils are in your PATH."
26+
exit 1
27+
fi
28+
done
29+
30+
# Ensure destination exists
31+
if [ ! -e "$OPENSNITCH_CONF_PATH" ]; then
32+
mkdir -pv "$OPENSNITCH_CONF_PATH" || exit 1
33+
fi
34+
35+
# Update asnames
36+
echo "******** Updating ${ASNAMES_FILE##*/}... ********"
37+
# Create backup
38+
[ -f "$ASNAMES_FILE" ] && mv -vf "$ASNAMES_FILE" "$ASNAMES_FILE.last"
39+
if pyasn_util_asnames.py -o "$ASNAMES_FILE"; then
40+
echo "Updated asnames data"
41+
else
42+
echo "Failed to update asnames data, restoring backup"
43+
# Restore backup upon failure
44+
mv -vf "$ASNAMES_FILE.last" "$ASNAMES_FILE"
45+
fi
46+
47+
# Update ipasn db
48+
echo "******** Updating ${IPASN_FILE##*/}... ********"
49+
# Create backup
50+
[ -f "${IPASN_FILE}.gz" ] && mv -vf "${IPASN_FILE}.gz" "${IPASN_FILE}.gz.last"
51+
# Clean up rib data if needed
52+
[ -e "$RIBDATA_FILE" ] && rm -vf "$RIBDATA_FILE"
53+
# Pull both ipv4 & ipv6
54+
# The resulting rib files typically include a date string in the name
55+
# use --filename to identify
56+
if pyasn_util_download.py --latestv46 --filename "$RIBDATA_FILE"; then
57+
echo "Downloaded ipasn data"
58+
if pyasn_util_convert.py --single "$RIBDATA_FILE" "$IPASN_FILE" --compress --no-progress; then
59+
echo "Converted ipasn data"
60+
else
61+
echo "Failed to convert ipasn data, restoring backup"
62+
mv -vf "${IPASN_FILE}.gz.last" "${IPASN_FILE}.gz"
63+
fi
64+
else
65+
echo "Failed to download ipasn data, restoring backup"
66+
mv -vf "${IPASN_FILE}.gz.last" "${IPASN_FILE}.gz"
67+
fi

0 commit comments

Comments
 (0)