Last active
November 28, 2023 12:04
-
-
Save alirezaomidi/9eeea3aa0a0a5a3404ea82f12741a475 to your computer and use it in GitHub Desktop.
Script to connect and disconnect to/from openconnect without pain
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 | |
# 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 |
Thanks for the contribution, but when connecting it disables the internet connection.
Thank you.
Damet garm Ali jun <3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, you helped me realize that I was using the default
kill
signal which is TERM which...according to openconnect manual, makes openconnect exit immediately without logging off or running vpnc-script and vpnc-script is responsible for restoring your route and dns which was not happening in my case making my internet connection run like in 90's on a telephone modem.