#!/bin/bash if [[ $EUID -eq 0 ]]; then root=1 fi tmpfile="/tmp/connectedto" vpnconfpath="/etc/openvpn/client" function helpertext () { echo "VPN Manager Help Text" echo "When connected yo have the following options" echo "stop - This stopps the VPN connection" echo "info|status - This shows the status of the connection" } function weAreConnected () { if [ -f $tmpfile ] then echo "We are connected to `cat $tmpfile`" # if [[ $1 == "stop" ]] # then # systemctl stop openvpn-client@`cat $tmpfile` # fi case $1 in "stop") if [[ $root -eq 1 ]] then echo "Stopping VPN Connection" systemctl stop openvpn-client@`cat $tmpfile` echo "Removing connectedto file" rm -f $tmpfile else sudo vpnctl $1 fi ;; "info") systemctl status openvpn-client@`cat $tmpfile` ;; "status") systemctl status openvpn-client@`cat $tmpfile` ;; *) helpertext ;; esac fi } function notConnected () { #TODO All funtions here require root so I should move this to a single check case $1 in "list") if [[ $root -eq 1 ]] then ls -alh $vpnconfpath | grep conf | awk '{print $9}' | cut -d '.' -f 1 else sudo vpnctl $1 fi ;; "connect") if [[ $root -eq 1 ]] then systemctl start openvpn-client@$2 echo $2 > $tmpfile else sudo vpnctl $1 $2 fi ;; *) echo "use 'vpn connect DESTINATION'" vpnctl list ;; esac } ## Check for tunnel device to see if we are connected if [ -d /sys/class/net/tun0 ] then weAreConnected $1 else notConnected $1 $2 fi