diff --git a/vpnctl b/vpnctl new file mode 100755 index 0000000..273fd54 --- /dev/null +++ b/vpnctl @@ -0,0 +1,87 @@ +#!/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 /etc/openvpn/client + 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 diff --git a/vpnnew.sh b/vpnnew.sh deleted file mode 100755 index 4d09710..0000000 --- a/vpnnew.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/bin/bash -if [[ $EUID -ne 0 ]]; then - echo "This script must be run as root" - exit 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" -} - -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") - echo "Stopping VPN Connection" - systemctl stop openvpn-client@`cat $tmpfile` - ;; - *) - helpertext -esac -fi - -} - -## Check for tunnel device to see if we are connected -if [ -d /sys/class/net/tun0 ] -then - weAreConnected $1 -else - connected=0 -fi