A VPN shell script for openconnect on Linux
A shell script for openconnect which allows:
- to define multiple VPN connections, using different protocols
- to run openconnect without entering the username and password
- to run in the background
- to authenticate with a certiftcate
- to check the status of the vpn connection
- added support for using different protocols
- added options (start, stop, status, restart)
- can check status of the vpn connection
This code is modified by sorinipate's repository: [vpn-up-for-openconnect].
And I made the following modifications:
- reformat all code style, refactor some variable name
- reformat all the space characters to [Tab] character, although I prefer to use space characters
- change the path of the PID file and log file, like "/run/xxxxx.pid", "/tmp/xxxxx.log"
- split VPN server configuration to "open-vpn-conf.sh" file
- added "install.sh" shell script
Last modified: shines77 / 2022-03-22
Copy the original configurtion file to open-vpn-my-conf.sh:
cp ./open-vpn-original-conf.sh ./open-vpn-my-conf.shEdit your configurtion file:
vim open-vpn-my-conf.shThe content is modified to like below:
# If you don't want to run in background, so make this false
BACKGROUND=true
# Company VPN
export VPN1_NAME="My Company VPN"
export VPN1_PROTOCOL="anyconnect"
export VPN1_HOST="vpn.mycompany.com"
export VPN1_AUTHGROUP="developers"
export VPN1_USER="sorin.ipate"
export VPN1_PASSWD="MyPassword"
# If you don't have server certificate so don't fill this
export VPN1_SERVER_CERTIFICATE="SHA1-OtherCharachters"Here just demonstrate how to use this script in Ubuntu server.
sudo apt-get update
sudo apt-get install openconnectTwo ways:
-
Pull this repository use git from here.
-
Or download the latest release, and upload it to your server, then extract it.
Copy the original configurtion file to open-vpn-my-conf.sh:
cp ./open-vpn-original-conf.sh ./open-vpn-my-conf.shAnd then, edit your configurtion file:
vim open-vpn-my-conf.shThe example configurtion file setting like "Sample VPN configuration" section above.
Finally, use this command to install, syntax is:
install.sh <folder_install_to>Example:
# Install to default folder: /usr/sbin
sudo ./install.sh
or
# Install to system /usr/bin folder
sudo ./install.sh /usr/bin
or
# Install to current user's bin folder
sudo mkdir ~/bin
sudo ./install.sh ~/binNote: The path of <folder_install_to> must be existed. If you don't specified <folder_install_to>, the default value is "/usr/sbin".
The installation steps are as follows:
# Your script root folder
cd /xxxxxx/yyyyy/vpn-shell-for-openconnect
sudo cp open-vpn-cmd.sh /usr/bin/open-vpn-cmd.sh
sudo cp open-vpn-my-conf.sh /usr/bin/open-vpn-conf.sh
sudo chmod +x /usr/bin/open-vpn-cmd.sh
alias open-vpn-cmd='/usr/bin/open-vpn-cmd.sh'open-vpn-cmd.sh startopen-vpn-cmd.sh stopopen-vpn-cmd.sh restartopen-vpn-cmd.sh status-
Please make sure you have
openconnectinstalled before moving on. Follow the instructions here. -
Copy the "
open-vpn-cmd.sh" and "open-vpn-conf.sh" file to the "~/bin" folder. -
Update the "
open-vpn-cmd.sh" file with the appropiate VPN connection information as shown above. -
Make an alias
alias open-vpn-cmd='~/bin/open-vpn-cmd.sh'inbashorzshshell. Follow the instructions here. -
Run
open-vpn-cmdto start and voila.