skip to Main Content

I am using a VPN server on a vps. The VPN service I use can of course go down for a few minutes or maybe even hours by accident or whatever. If it happens, I would like to quickly reconnect to another vpn-server with the command "vpnapp reconnect".

Does anyone know how to do a bash script I will run every minute via cronjob that:

  1. Pings google.com

2A) If it can be reached, do nothing

2B) If it cannot be reached – which would mean the vpn server is down/not working – enter terminal command "vpnapp reconnect" which will make the vpn application reconnect to next available server they offer.

Would be so thankful if anyone could help me. I have ZERO programming knowledge. To me this is pure science.

Thanks in advance.
Oski

2

Answers


  1. Chosen as BEST ANSWER

    Can it be this simple?

    if ping -c 4 google.com ; then exit ; else vpnapp reconnect ; fi
    

    So if 4 packages are received, meaning google.com can be reached it will just exit the bash script.

    if not, then it will just enter vpnapp reconnect as a command in the terminal... reconnecting the vpn.

    or does it need to be "vpnapp reconnect" embedded in "?

    Lol i am so weak at this. I really dont trust my own suggestion anyway so would love to hear a real programmer speak. Thanks!!


  2. You don’t need a bash script, you can just add this cron job (with crontab -e):

    * * * * * ping -c2 google.com || vpnapp reconnect
    
    • * * * * * Run the command every minute.
    • ping -c2 google.com Send 2 pings to google.com.
    • || vpnapp reconnect mean that if ping failled it will run vpnapp reconnect
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search