shines77 / vpn-shell-for-openconnect

A VPN shell script for openconnect on Linux

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

vpn-shell-for-openconnect

A VPN shell script for openconnect on Linux

Features

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

What's new

  • added support for using different protocols
  • added options (start, stop, status, restart)
  • can check status of the vpn connection

Last modifications

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

Sample VPN configuration

Copy the original configurtion file to open-vpn-my-conf.sh:

cp ./open-vpn-original-conf.sh ./open-vpn-my-conf.sh

Edit your configurtion file:

vim open-vpn-my-conf.sh

The 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"

How to install and use

Here just demonstrate how to use this script in Ubuntu server.

1. Install openconnect

sudo apt-get update
sudo apt-get install openconnect

2. Get this script

Two ways:

3. Install this script

Copy the original configurtion file to open-vpn-my-conf.sh:

cp ./open-vpn-original-conf.sh ./open-vpn-my-conf.sh

And then, edit your configurtion file:

vim open-vpn-my-conf.sh

The 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 ~/bin

Note: 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'

4. VPN usage

4.1. Connect VPN

open-vpn-cmd.sh start

4.2. Disconnect VPN

open-vpn-cmd.sh stop

4.3. Restart VPN

open-vpn-cmd.sh restart

4.4. Query VPN status

open-vpn-cmd.sh status

Original usage

Run VPN shell script

  1. Please make sure you have openconnect installed before moving on. Follow the instructions here.

  2. Download the latest release.

  3. Copy the "open-vpn-cmd.sh" and "open-vpn-conf.sh" file to the "~/bin" folder.

  4. Update the "open-vpn-cmd.sh" file with the appropiate VPN connection information as shown above.

  5. Make an alias alias open-vpn-cmd='~/bin/open-vpn-cmd.sh' in bash or zsh shell. Follow the instructions here.

  6. Run open-vpn-cmd to start and voila.

About

A VPN shell script for openconnect on Linux

License:MIT License


Languages

Language:Shell 100.0%