Last active
September 16, 2018 19:14
-
-
Save jackm/7df38d6df74a78de12834f06c3a67e92 to your computer and use it in GitHub Desktop.
Wrapper script for managing connection to a VPN server using openconnect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Connect to a VPN server using openconnect | |
prog_name=$(basename $0) | |
pid_file="/var/run/openconnect" | |
default_server="https://vpn.example.com/" | |
help () { | |
echo "Usage: $prog_name [-c server] [-d]" | |
echo | |
echo "Options" | |
echo " -c, --connect <server> Connect to the specified VPN server" | |
echo " -d, --disconnect Disconnect the active VPN connection" | |
echo | |
} | |
connect () { | |
if [ -n "$1" ]; then | |
server=$1 | |
else | |
server=$default_server | |
fi | |
if [ -f $pid_file ] && ps -p $(< $pid_file) >/dev/null 2>&1; then | |
echo "Already connected to VPN server" | |
exit 1 | |
fi | |
sudo openconnect -b --pid-file=$pid_file $server | |
} | |
disconnect () { | |
if [ -f $pid_file ]; then | |
sudo pkill -SIGINT -F $pid_file | |
else | |
echo "Not connected to VPN server" | |
exit 1 | |
fi | |
} | |
subcommand="$1" | |
case $subcommand in | |
-h|--help|"") | |
help | |
;; | |
-c|--connect|connect) | |
shift | |
connect $@ | |
;; | |
-d|--disconnect|disconnect) | |
disconnect | |
;; | |
*) | |
echo "Error: '$subcommand' is not a known command." >&2 | |
echo " Run '$prog_name --help' for a list of known commands." >&2 | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment