Skip to content

Instantly share code, notes, and snippets.

@ppproxy
Last active October 29, 2021 08:46
Show Gist options
  • Save ppproxy/90f6ffd98fc2677bad2380b79142a447 to your computer and use it in GitHub Desktop.
Save ppproxy/90f6ffd98fc2677bad2380b79142a447 to your computer and use it in GitHub Desktop.
update the dynamic dns endpoint address in wireguard when the remote ip has changed
#!/bin/bash
# update_WG_DDNS_endpoint
# update the dynamic dns endpoint address in wireguard when the remote ip has changed
set -e
WG_IFACE="wg0"
WG_PEER="your peer"
WG_ENDPOINT="ddns.test.name:port"
PUBLICIP="/tmp/publicIP"
[ -f "$PUBLICIP" ] || touch "$PUBLICIP"
# get current DDNS IP address
CURRENT_IP=$(host -4 -t A ddns.test.name | awk '{print $5}' 2>/dev/null)
echo "Current ednpoint IP is" $CURRENT_IP
# validate IP address
if [[ ! $CURRENT_IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
exit 1
fi
PREVIOUS_IP=$(tail -n 1 "$PUBLICIP")
# update endpoint
if [ "$CURRENT_IP" != "$PREVIOUS_IP" ]; then
echo "Updating endpoint IP to $CURRENT_IP"
echo $CURRENT_IP >"$PUBLICIP"
# update wireguard endpoint
sudo wg set $WG_IFACE peer $WG_PEER endpoint $WG_ENDPOINT
else
echo "IP address has not changed"
fi
echo "##### All Done! #####"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment