Skip to content

Instantly share code, notes, and snippets.

@janlay
Created March 25, 2019 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janlay/26da391c0e016b309b0a89043974d07c to your computer and use it in GitHub Desktop.
Save janlay/26da391c0e016b309b0a89043974d07c to your computer and use it in GitHub Desktop.
ChinaDNS Updater for OpenWRT
#!/bin/bash
FILE=/etc/chinadns_chnroute.txt
TEMP_FILE="$FILE.tmp"
TELEGRAM_BOT=/root/scripts/telegram-bot
SOURCE="https://raw.githubusercontent.com/ym/chnroutes2/master/chnroutes.txt"
LOGFILE=/root/scripts/chinadns-updater.log
echo -e "\nChinaDNS Updater started at `date`." >> $LOGFILE
echo "Current routes: `wc -l "$FILE" | cut -d' ' -f1`." >> $LOGFILE
echo "Fetching networks from $SOURCE ..." >> $LOGFILE
if ! curl -sfk "$SOURCE" -o "$TEMP_FILE"; then
echo "Error returned, check the source." >> $LOGFILE
exit 1
fi
grep '#' "$TEMP_FILE" | cut -d' ' -f2- > "$FILE.stats"
cat "$FILE.stats" >> $LOGFILE
sed -i "/^#/d" "$TEMP_FILE"
echo "Backing up $FILE ..." >> $LOGFILE
cp "$FILE" "$FILE.bak"
echo "Restarting ChinaDNS service ..." >> $LOGFILE
mv "$TEMP_FILE" "$FILE"
/etc/init.d/chinadns restart
$TELEGRAM_BOT "*ChinaDNS routes updated*:
`cat $FILE.stats`"
rm $FILE.stats
echo "All done." >> $LOGFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment