Last active
July 6, 2020 07:57
-
-
Save cmer/4ec912134758a0bac54ea65a22d29c5e to your computer and use it in GitHub Desktop.
Script to update Wireguard routes in DD-WRT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
while : | |
do | |
WGPROC=$(wg) | |
WGIF=$(ip route show gateway | grep -io oet1) | |
WGSERVER=$(/usr/sbin/nvram get oet1_rem0) | |
WANGWY=$(/usr/sbin/nvram get wan_gateway) | |
WANIF=$(/usr/sbin/nvram get wan_iface) | |
if ! [[ $WGSERVER =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then | |
echo "Resolving hostname $WGSERVER..." | |
WGSERVER=$(nslookup $WGSERVER 1.1.1.1 | awk '/Name/,/END/' | grep Address | awk -v RS='([0-9]+\\.){3}[0-9]+' 'RT{print RT}' | head -n 1) | |
echo "Resolved to $WGSERVER" | |
fi | |
HOST=$(ip route | grep -E "($WGSERVER.*$WANGWY)") | |
until /usr/sbin/nvram get wanup | grep -q 1 ; do | |
echo "Waiting for WAN to initialize..." | |
sleep 10 | |
done | |
if [ "$WGPROC" ] ; then | |
echo "Wireguard is running. Checking routes..." | |
if [ ! "$WGIF" ] || [ ! "$HOST" ] ; then | |
echo "Routes missing. Correcting..." | |
route add -host $WGSERVER gw $WANGWY dev $WANIF | |
route del default | |
route add default dev oet1 | |
ip route flush cache | |
echo "Done" | |
echo "" | |
curl ipinfo.io | |
echo "" | |
else | |
echo "Routes are correct" | |
fi | |
else | |
echo "Wireguard is not running" | |
fi | |
sleep 60 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment