From fbebdb8bef78c57af227efeb964032e1da875d8f Mon Sep 17 00:00:00 2001 From: Thomas Schmauder Date: Fri, 10 Feb 2023 08:56:26 +0100 Subject: [PATCH] v1 init --- v1.sh | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 v1.sh diff --git a/v1.sh b/v1.sh new file mode 100644 index 0000000..ce7e4a3 --- /dev/null +++ b/v1.sh @@ -0,0 +1,68 @@ +#!/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 + +# 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_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