#!/bin/bash # Get current status of a VPN connection with options to connect/disconnect. # Working with OpenConnect, but can work with any executable VPN. Commands # that require admin permissions should be whitelisted with 'visudo', e.g.: # #joesmith ALL=(ALL) NOPASSWD: /path/to/always-on-openconnect-vpn/run-vpn.sh #joesmith ALL=(ALL) NOPASSWD: /usr/bin/killall -2 openconnect # VPN Status # v1.0 # Jesse Jarzynka # jessejoe # Displays status of a VPN interface with option to connect/disconnect. # http://i.imgur.com/RkmptwO.png VPN_CONNECTED="ifconfig | egrep -A1 utun3 | grep inet" case "$1" in connect) terminal-notifier -title "VPN" -message "Connecting..." -sender "SwiftBar" cd /path/to/always-on-openconnect-vpn/ sudo /path/to/always-on-openconnect-vpn/run-vpn.sh &> /tmp/vpn.log & until eval "$VPN_CONNECTED"; do sleep 1; done terminal-notifier -title "VPN" -message "Connected!" -sender "SwiftBar" ;; disconnect) eval "sudo killall -2 openconnect" until [ -z "$(eval "$VPN_CONNECTED")" ]; do sleep 1; done terminal-notifier -title "VPN" -message "Disconnected" -sender "SwiftBar" ;; esac if [ -n "$(eval "$VPN_CONNECTED")" ]; then echo "VPN ✔" echo '---' echo "Disconnect VPN | bash='$0' param1=disconnect terminal=false refresh=true" exit else echo "VPN ✘" echo '---' echo "Connect VPN | bash='$0' param1=connect terminal=false refresh=true" exit fi