Skip to content

Instantly share code, notes, and snippets.

@alirezaomidi
Last active November 28, 2023 12:04
Show Gist options
  • Star 55 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save alirezaomidi/9eeea3aa0a0a5a3404ea82f12741a475 to your computer and use it in GitHub Desktop.
Save alirezaomidi/9eeea3aa0a0a5a3404ea82f12741a475 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
@FabienPapet
Copy link

Hello,
Can you please post an example of maker.txt content ?

regards.

@jesbrd
Copy link

jesbrd commented Jul 9, 2018

Agreed - thank you for the script. Can you please provide an example of vpnmakers.txt?

@yazdan
Copy link

yazdan commented Oct 3, 2019

I think vpnmakers.txt might be something like this
serveraddress -u uername
just openconnect's arguments

@peymanslh
Copy link

Its a simple text file like this:

yes
VPN_USERNAME
VPN_PASSWORD

Use yes if your VPN server is insecure and openconnect wants an agreement from you.

@alirezaomidi
Copy link
Author

Its a simple text file like this:

yes
VPN_USERNAME
VPN_PASSWORD

Use yes if your VPN server is insecure and openconnect wants an agreement from you.

yeap. That's the right text file. 👍

@rmeyersnrec
Copy link

Thank you for providing this.

@alirezaomidi
Copy link
Author

Thank you for providing this.

YW.

@mchen2000
Copy link

Thank you for this. It works like a charm.

@loverdeveloper
Copy link

it's amazing,
thanks alireza jan :boos:

@jedrekdomanski
Copy link

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.

@leeed
Copy link

leeed commented Mar 12, 2021

Thanks for the contribution, but when connecting it disables the internet connection.

@abduttawab
Copy link

Thank you for providing this.

YW.

Hello. Thanks for sharing this. I am trying to do this on Windows but not working can you please help me.

First of all "-b" command not found

It is not getting the information from vpnmakers.txt

Capture

@salehmontazeran
Copy link

Thank you.

@AliGhelichkhani80
Copy link

Damet garm Ali jun <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment