Skip to content

Instantly share code, notes, and snippets.

@keuv-grvl
Last active August 21, 2018 00:31
Show Gist options
  • Save keuv-grvl/2163bb8f4b75b02c1223ac49d73505d8 to your computer and use it in GitHub Desktop.
Save keuv-grvl/2163bb8f4b75b02c1223ac49d73505d8 to your computer and use it in GitHub Desktop.
Toggle a given VPN, eventually ask for password
#!/usr/bin/env bash
# usage: toggle-vpn.sh vpn-name
set -e # this script exits on error
set -o pipefail # a pipeline returns error code if any command fails
set -u # unset variables as an error, and immediately exit
command -v nmcli > /dev/null # ensure nmcli is installed
VPN=$1
# ensure we do not try to shutdown ethernet or wifi
nmcli con | grep vpn | cut -d' ' -f1 | grep "^${VPN}$" > /dev/null
# try to shutdown, otherwise try to connect without passwd, otherwise ask for it
nmcli con down $VPN || nmcli con up $VPN || nmcli --ask con up $VPN ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment