Skip to content

Instantly share code, notes, and snippets.

@tywkeene
Created October 24, 2025 18:41
Show Gist options
  • Select an option

  • Save tywkeene/f58c2c693da87fed8829a96a36383704 to your computer and use it in GitHub Desktop.

Select an option

Save tywkeene/f58c2c693da87fed8829a96a36383704 to your computer and use it in GitHub Desktop.
Wireguard wg-quick script to rotate tunnels.
#!/bin/sh
# Script to rotate wireguard tunnels. Basically just calls wg-quick
# Works best if you have many tunnel configuration files in /usr/local/etc/wireguard
# (or whereever wg puts them on your system).
#
# Works in cron:
# */15 * * * * /usr/local/scripts/tunnel.sh >/dev/null 2>&1
#
# Note: ``>/dev/null 2>&1`` is recommended to avoid spamming logs/root mail
current_tunn=$(ifconfig | awk -F: '/^wg[0-9]+:/{print $1}')
new_conf=$(find /usr/local/etc/wireguard -type f -name '*.conf' | shuf -n1 -r)
new_tunn=$(basename -s .conf $new_conf)
[ -z "$current_tunn" ] && wg-quick down $current_tunn
wg-quick up $new_conf
TUNNEL_IPINFO=1
TUNNEL_NOTIFY=1
[ -n "$TUNNEL_IPINFO" ] && message=$(curl -s ipinfo.io | jq -r '[.city,.region] | join(", ")')
[ -n "$TUNNEL_NOTIFY" ] && DISPLAY=:0 notify-send "Switched tunnel" "$current_tunn -> $new_tunn\n$message"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment