What's new

[SCRIPT] sh to check vpn client status and auto reconnect if disconnected

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

dutuka

New Around Here
Hello community,
my vpn service provider does not support keep alive in their setting, so i need another way to check if vpn client is connected and auto reconnect if it is disconnected, a sh script should do the job.

The scipt should do something like this:

nvram get vpn_client1_state # Check VPN client status
if response code is not 2 then # if vpn client is not connected then
service stop_vpnclient1 # stop vpn client
sleep 3s # pause 3 seconds
service start_vpnclient1 # start/connect vpn client

loop these commands for vpnclient[1-5]


My router is AX56U and can set up to 5 vpn clients, the status codes are:
2=connected
1= connecting
0=disconnected
-1= failed to connect

I dont know how to write sh script and hope someone can help me to make a working script, so that i can shedule it with crontab. Many thanks.
 
Last edited:
I figure it out, this is my script.

Code:
#!/bin/sh
VPNclient1_Status=$(nvram get vpn_client1_state)
if [ "$VPNclient1_Status" -ne 2 ]
then
service stop_vpnclient1 && sleep 3s && service start_vpnclient1
fi

VPNclient2_Status=$(nvram get vpn_client2_state)
if [ "$VPNclient2_Status" -ne 2 ]
then
service stop_vpnclient2 && sleep 3s && service start_vpnclient2
fi

VPNclient3_Status=$(nvram get vpn_client3_state)
if [ "$VPNclient3_Status" -ne 2 ]
then
service stop_vpnclient3 && sleep 3s && service start_vpnclient3
fi

VPNclient4_Status=$(nvram get vpn_client4_state)
if [ "$VPNclient4_Status" -ne 2 ]
then
service stop_vpnclient4 && sleep 3s && service start_vpnclient4
fi

VPNclient5_Status=$(nvram get vpn_client5_state)
if [ "$VPNclient5_Status" -ne 2 ]
then
service stop_vpnclient5 && sleep 3s && service start_vpnclient5
fi
 
Last edited:
OVPN Client Connect Script

Run this script with cru set to every minute to check ovpn client connection and reconnect if a client is disconnected.

Download from here:

 
Last edited:
This script can also work for another router, where exit code for connected state NOT 2 is

Code:
#!/bin/sh
if [ -z "$(pidof vpnclient1)" ]
then
   service restart_vpnclient1
fi

if [ -z "$(pidof vpnclient2)" ]
then
   service restart_vpnclient2
fi

if [ -z "$(pidof vpnclient3)" ]
then
   service restart_vpnclient3
fi

if [ -z "$(pidof vpnclient4)" ]
then
   service restart_vpnclient4
fi

if [ -z "$(pidof vpnclient5)" ]
then
   service restart_vpnclient5
fi
 

Similar threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top