Skip to content

Instantly share code, notes, and snippets.

@tx0dev
Created February 19, 2011 02:00
Show Gist options
  • Save tx0dev/834759 to your computer and use it in GitHub Desktop.
Save tx0dev/834759 to your computer and use it in GitHub Desktop.
Simple OpenVPN connection manager.
#!/bin/bash
# File location
OVPN_CONFIG="$HOME/local/settings/acanac.ovpn"
OVPN_USER="$HOME/local/settings/acanac.user"
OVPN_PID="/tmp/acanac.pid"
OVPN_LOG="/tmp/acanac.log"
IP_SERVICE="http://myip.dnsomatic.com"
echo "OpenVPN connection manager"
#### Ask yesno question. ####
# Usage: yesno OPTIONS QUESTION
# Options:
# --timeout N Timeout if no input seen in N seconds.
# --default ANS Use ANS as the default answer on timeout or
# if an empty answer is provided.
# Exit status is the answer.
function yesno()
{
local ans
local ok=0
local timeout=0
local default
local t
while [[ "$1" ]]
do
case "$1" in
--default)
shift
default=$1
if [[ ! "$default" ]]; then error "Missing default value"; fi
t=$(tr '[:upper:]' '[:lower:]' <<<$default)
if [[ "$t" != 'y' && "$t" != 'yes' && "$t" != 'n' && "$t" != 'no' ]]; then
error "Illegal default answer: $default"
fi
default=$t
shift
;;
--timeout)
shift
timeout=$1
if [[ ! "$timeout" ]]; then error "Missing timeout value"; fi
if [[ ! "$timeout" =~ ^[0-9][0-9]*$ ]]; then error "Illegal timeout value: $timeout"; fi
shift
;;
-*)
error "Unrecognized option: $1"
;;
*)
break
;;
esac
done
if [[ $timeout -ne 0 && ! "$default" ]]; then
error "Non-zero timeout requires a default answer"
fi
if [[ ! "$*" ]]; then error "Missing question"; fi
while [[ $ok -eq 0 ]]
do
if [[ $timeout -ne 0 ]]; then
if ! read -t $timeout -p "$*" ans; then
ans=$default
echo
else
# Turn off timeout if answer entered.
timeout=0
if [[ ! "$ans" ]]; then ans=$default; fi
fi
else
read -p "$*" ans
if [[ ! "$ans" ]]; then
ans=$default
else
ans=$(tr '[:upper:]' '[:lower:]' <<<$ans)
fi
fi
if [[ "$ans" == 'y' || "$ans" == 'yes' || "$ans" == 'n' || "$ans" == 'no' ]]; then
ok=1
fi
if [[ $ok -eq 0 ]]; then warning "Valid answers are: yes y no n"; fi
done
[[ "$ans" = "y" || "$ans" == "yes" ]]
}
# Get PID
VPNPID=`pgrep openvpn`
# Check if there is a openvpn process running.
if [ ! -z "$VPNPID" ]
then
echo "There is already an instance of OpenVPN running."
echo "PID: $VPNPID"
if yesno --timeout 3 --default no "Stop the VPN?(yes,NO) "
then
# Terminate the VPN via a SIGTERM
echo "Terminating OpenVPN process"
sudo kill $VPNPID
else
# Nothing to do
echo -e "Exiting."
fi
exit 0
else
# No VPN running, shall we start one?
echo -n "No OpenVPN instance. "
if yesno --timeout 2 --default yes "Start the VPN?(YES/no)"
then
# Request Sudo privileges (so it won't break the display)
sudo -v
echo -n "Starting ..."
else
echo "Exiting."
exit 0
fi
fi
# Get current public IP
DIRECTIP=`curl -s $IP_SERVICE`
# Start the openvpn process
sudo openvpn --config $OVPN_CONFIG --auth-user-pass $OVPN_USER --writepid $OVPN_PID --log $OVPN_LOG &
# Add a bit of sleep otherwise the next step uses the old file.
sleep 1
# Wait for complete connection
echo -n "Initialization..."
while [ `sudo grep -c 'Initialization Sequence Completed' $OVPN_LOG` -eq 0 ]
do
echo -en '/\b' ; sleep 0.1
echo -en '-\b' ; sleep 0.1
echo -en '\\\b' ; sleep 0.1
echo -en '|\b' ; sleep 0.1
done
echo -en " Complete!\n"
# For safety, let's revoke sudo privileges
sudo -k
# Get (hopefully) new public IP
VPNIP=`curl -s $IP_SERVICE`
echo "NETWORK IP: $DIRECTIP"
echo "VPN IP: $VPNIP"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment