10-wg-reload.sh
· 2.3 KiB · Bash
Raw
#!/bin/bash
##
## Reloads Wireguard tunnel when a physical interface change happens
## Intended for use with always active full tunnels
##
## Also when using a second VPN, disables the WG tunnel when
## enabling the alternate and vice versa
##
# interface names as shown by 'ip a'
WG_INTERFACE=''
ETH_INTERFACE=''
WIFI_INTERFACE=''
ALT_VPN=''
if [[ "$1" == "$ETH_INTERFACE" ]] || [[ "$1" == "$WIFI_INTERFACE" ]] && [[ "$2" == "up" ]]; then
echo "running $0: $1 $2" | systemd-cat -t nm-dispatcher
sleep 2
nmcli conn up $WG_INTERFACE
resolvectl flush-caches
elif [[ "$1" == "$ALT_VPN" ]] && [[ "$2" == "up" ]]; then
echo "running $0: $1 $2" | systemd-cat -t nm-dispatcher
nmcli conn down $WG_INTERFACE
elif [[ "$1" == "$ALT_VPN" ]] && [[ "$2" == "down" ]]; then
echo "running $0: $1 $2" | systemd-cat -t nm-dispatcher
nmcli conn up $WG_INTERFACE
fi
exit 0
| 1 | #!/bin/bash |
| 2 | |
| 3 | ## |
| 4 | ## Reloads Wireguard tunnel when a physical interface change happens |
| 5 | ## Intended for use with always active full tunnels |
| 6 | ## |
| 7 | ## Also when using a second VPN, disables the WG tunnel when |
| 8 | ## enabling the alternate and vice versa |
| 9 | ## |
| 10 | |
| 11 | # interface names as shown by 'ip a' |
| 12 | WG_INTERFACE='' |
| 13 | ETH_INTERFACE='' |
| 14 | WIFI_INTERFACE='' |
| 15 | ALT_VPN='' |
| 16 | |
| 17 | if [[ "$1" == "$ETH_INTERFACE" ]] || [[ "$1" == "$WIFI_INTERFACE" ]] && [[ "$2" == "up" ]]; then |
| 18 | echo "running $0: $1 $2" | systemd-cat -t nm-dispatcher |
| 19 | sleep 2 |
| 20 | nmcli conn up $WG_INTERFACE |
| 21 | resolvectl flush-caches |
| 22 | elif [[ "$1" == "$ALT_VPN" ]] && [[ "$2" == "up" ]]; then |
| 23 | echo "running $0: $1 $2" | systemd-cat -t nm-dispatcher |
| 24 | nmcli conn down $WG_INTERFACE |
| 25 | elif [[ "$1" == "$ALT_VPN" ]] && [[ "$2" == "down" ]]; then |
| 26 | echo "running $0: $1 $2" | systemd-cat -t nm-dispatcher |
| 27 | nmcli conn up $WG_INTERFACE |
| 28 | fi |
| 29 | |
| 30 | exit 0 |