69 lines
3.3 KiB
Bash
69 lines
3.3 KiB
Bash
#!/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: /usr/local/bin/openconnect
|
|
#joesmith ALL=(ALL) NOPASSWD: /usr/bin/killall -2 openconnect
|
|
|
|
# <xbar.title>VPN Status</xbar.title>
|
|
# <xbar.version>v1.0</xbar.version>
|
|
# <xbar.author>Jesse Jarzynka</xbar.author>
|
|
# <xbar.author.github>jessejoe</xbar.author.github>
|
|
# <xbar.desc>Displays status of a VPN interface with option to connect/disconnect.</xbar.desc>
|
|
# <xbar.image>http://i.imgur.com/RkmptwO.png</xbar.image>
|
|
|
|
VPN_EXECUTABLE=$(which openconnect)
|
|
VPN_EXECUTABLE_PARAMS="--servercert pin-sha256:NFR9+lG+7SvvEqnraijD1l5R1ShK9KKtwYjqedfx78U= --protocol=fortinet" # Optional
|
|
VPN_HOST="vpn.bib.de"
|
|
VPN_USERNAME="$2"
|
|
# A command that will result in your VPN password. Recommend using
|
|
# "security find-generic-password -g -a foo" where foo is an account
|
|
# in your OSX Keychain, to avoid passwords stored in plain text
|
|
GET_VPN_PASSWORD="security find-generic-password -g -a $VPN_USERNAME 2>&1 >/dev/null | cut -d'\"' -f2"
|
|
|
|
# Command to determine if VPN is connected or disconnected
|
|
VPN_CONNECTED="ifconfig | egrep -A1 'inet 172.[123][0-9]' |cut -d' ' -f2"
|
|
# Command to run to disconnect VPN
|
|
VPN_DISCONNECT_CMD="sudo killall -2 openconnect"
|
|
# Get IP of Current VPN Tunnel
|
|
IP=$(ifconfig | egrep -A1 'inet 172.[123][0-9]' |cut -d' ' -f2)
|
|
|
|
case "$1" in
|
|
connect)
|
|
VPN_PASSWORD=$(eval "$GET_VPN_PASSWORD")
|
|
# VPN connection command, should eventually result in $VPN_CONNECTED,
|
|
# may need to be modified for VPN clients other than openconnect
|
|
echo "$VPN_PASSWORD" | sudo "$VPN_EXECUTABLE" $VPN_EXECUTABLE_PARAMS --user "$VPN_USERNAME" --passwd-on-stdin "$VPN_HOST" &> /dev/null &
|
|
|
|
IP=$(ifconfig | egrep -A1 'inet 172.[123][0-9]' |cut -d' ' -f2)
|
|
while [ -z $IP ]; do sleep 0.5 ; IP=$(ifconfig | egrep -A1 'inet 172.[123][0-9]' |cut -d' ' -f2) ; done
|
|
msg='display notification "Erfolgreich verbunden \nConnected User: '$VPN_USERNAME'" with title "OpenFortiVPN" subtitle "Deine IP lautet: '$IP'" sound name "Brise"'
|
|
errmsg='display notification "Verbindungsversuch nicht erfolgreich" with title "OpenFortiVPN" subtitle "Schade" sound name "Brise"'
|
|
if [[ $IP =~ 172 ]] ; then osascript -e "$msg" ; else osascript -e "$errmsg" ; fi
|
|
# Wait for connection so menu item refreshes instantly
|
|
until eval "$VPN_CONNECTED"; do sleep 1; done
|
|
;;
|
|
disconnect)
|
|
eval "$VPN_DISCONNECT_CMD"
|
|
# Wait for disconnection so menu item refreshes instantly
|
|
until [ -z "$(eval "$VPN_CONNECTED")" ]; do sleep 1; done
|
|
osascript -e 'display notification "VPN Tunnel erfolgreich geschlossen" with title "OpenFortiVPN" subtitle "Mach Feierabend" sound name "Submarine"'
|
|
;;
|
|
esac
|
|
|
|
if [ -n "$(eval "$VPN_CONNECTED")" ]; then
|
|
echo "VPN ✔ | color=green"
|
|
echo '---'
|
|
echo "Disconnect VPN | bash='$0' param1=disconnect terminal=false refresh=true"
|
|
echo "User: $(ps -ef | grep -e '--user\ ' | cut -d' ' -f 33)"
|
|
echo "IP: $IP"
|
|
exit
|
|
else
|
|
echo "VPN ❌ | color=red"
|
|
echo '---'
|
|
echo "Connect VPN | bash='$0' param1=connect param2=alessa.mielemeier@edu-up.de terminal=false refresh=true"
|
|
exit
|
|
fi
|