Skip to content

Instantly share code, notes, and snippets.

@wernerjoss
Created December 11, 2023 17:09
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 wernerjoss/a6533c573fb19ecaa808482bd668b5fa to your computer and use it in GitHub Desktop.
Save wernerjoss/a6533c573fb19ecaa808482bd668b5fa to your computer and use it in GitHub Desktop.
toggle wireguard interface status, if connected: disconnect, and vice versa
#!/bin/bash
# toggle wireguard interface status, if connected: disconnect, and vice versa
# wernerjoss 11.11.23
wgclient="orion" # default
while getopts ":c:v:" opt; do
case $opt in
c)
wgclient="$OPTARG"
;;
v)
verbose="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
if [ ! -z $verbose ]; then
echo $wgclient
fi
status=$(nmcli -o d | grep $wgclient | awk '{print $1}')
if [ ! -z $verbose ]; then
echo $status
fi
if [ $status == $wgclient ]; then
echo "$wgclient is up, disconnecting"
wg-quick down $wgclient
else
echo "$wgclient is down, connecting"
wg-quick up $wgclient
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment