How to make vpnclient connect to vpnserver

Amazon Linux
Reading Time: 2 minutes



I introduced how to setup VPN by these articles.



In this article I will introduce how to make vpnclient connect to vpnserver when start up.

How to do

I implemented this shell script (bash) /opt/vpnclient/vpnclient.sh.

#!/bin/bash

status(){
        echo "==========status======="
}

start() {
        echo "==========start==========="
        /opt/vpnclient/vpnclient start
        echo -e $'\n' | /opt/vpnclient/vpncmd /CLIENT /CMD AccountConnect virtual_connection
        ip address add 192.168.100.150/24 dev vpn_virtual_nic
}

stop() {
        echo "===========stop============"
        echo -e $'\n' | /opt/vpnclient/vpncmd /CLIENT /CMD AccountDisconnect virtual_connection
        /opt/vpnclient/vpnclient stop
}

restart() {
        stop;
        echo "sleeping.........";
        sleep 3;
        start;
}
case "$1" in
        'start')
                start
                ;;
        'stop')
                stop
                ;;
        'status')
                status
                ;;
        'restart')
                restart
                ;;
        *)
                echo "usage: $0 {start|stop|restart|status}"
                exit 1
                ;;
esac



Normally vpnclient needs user input, but vpnclient has command line options to avoid user inputs and can be automated.

This is official site.
If you are interested in please have a look.
– 6.2.3 is which I would like you to see.

6.2 General Usage of vpncmd - SoftEther VPN Project



Furthermore I passed “Enter” key code by echo -e $’\n’ to skip user inputs for selecting which vpnclient to be connected (see below).

sudo /opt/vpnclient/vpncmd
vpncmd command - SoftEther VPN Command Line Management Utility
SoftEther VPN Command Line Management Utility (vpncmd command)
Version 4.39 Build 9772   (English)
Compiled 2022/04/26 18:00:50 by buildsan at crosswin
Copyright (c) SoftEther VPN Project. All Rights Reserved.

By using vpncmd program, the following can be achieved. 

1. Management of VPN Server or VPN Bridge 
2. Management of VPN Client
3. Use of VPN Tools (certificate creation and Network Traffic Speed Test Tool)

Select 1, 2 or 3: 2

Specify the host name or IP address of the computer that the destination VPN Client is operating on. 
If nothing is input and Enter is pressed, connection will be made to localhost (this computer).
Hostname of IP Address of Destination: 



I updated /etc/systemd/system/vpnclient.service to call this vpnclient.sh like below.

[Unit]
Description=SoftEther VPN Client
After=network.target

[Service]
ExecStart=/opt/vpnclient/vpnclient.sh start
ExecStop=/opt/vpnclient/vpnclient.sh stop
Type=forking
Restart=always

[Install]
WantedBy=graphical.target



That’s it.

If everything is successful, then vpnclient will automatically connect to vpnserver after boot up.

Conclusion

How was it?

This is small but very useful! isn’t it?

Comments

Copied title and URL