Skip to content

Instantly share code, notes, and snippets.

@skovtunenko
Forked from jesbrd/openconnect.sh
Created June 20, 2019 06:58
Show Gist options
  • Save skovtunenko/73fd00b59a47dd2368240d0da9e619c4 to your computer and use it in GitHub Desktop.
Save skovtunenko/73fd00b59a47dd2368240d0da9e619c4 to your computer and use it in GitHub Desktop.
Script to connect and disconnect to/from openconnect without pain
#!/bin/bash
# This script connects the computer to a vpn server using openconnect without pain
prog_name=$(basename $0)
# CHANGE YOUR_VPN_SERVER_DOMAIN to the VPN server you know like example.com
domain=YOUR_VPN_SERVER_DOMAIN
function help {
echo "Usage: $prog_name [-c server] [-d]"
echo
echo "Options"
echo " -c, --connect <subdomain> Connect to the specified VPN server (subdomain.domain)"
echo " -d, --disconnect Disconnect the running VPN"
echo
}
function connect {
server=$1.$domain
echo "Connecting to $server..."
sudo openconnect -b $server < ~/Documents/vpnmakers.txt
}
function disconnect {
echo "Disconnecting..."
sudo pkill -SIGINT openconnect
# Remove default gateway route rule when there is already a PPTP connection
# Uncomment line below if your computer is connected to internet through a PPTP connection
ip r | grep ppp0 && ip r | grep default | head -n1 | xargs sudo ip r del
}
subcommand=$1
case $subcommand in
"" | "-h" | "--help")
help
;;
"-c" | "--connect")
shift
connect $@
;;
"-d" | "--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