Script to Automatically Detect and Restart Linux PPTP Client

Script to Automatically Detect and Restart Linux PPTP Client

Category : How-to

Get Social!

Linux penguinThe default PPTP client for Linux does not automatically start on boot, or restart on a failed or dropped connection. I have written a short script to ping your VPN server gateway IP address and start the PPTP client if a ping does not succeed.

See my other post if you have not yet set up your PPTP VPN client.

Create this script and make it executable:

vi /root/cron/pptp_cron.sh
chmod +x /root/cron/pptp_cron.sh

Add the below script to the file and change the following attributes for your own values:

  • your-vpn-host-or-ip-address
  • your-vpn-username
  • your-vpn-password
#!/bin/bash

HOST=your-vpn-host-or-ip-address
PPTPUSER=your-vpn-username
PPTPPASS=your-vpn-password

DATE=`date`
PINGRES=`ping -c 2 $HOST`
PLOSS=`echo $PINGRES : | grep -oP '\d+(?=% packet loss)'`
echo "$DATE : Loss Result : $PLOSS"

if [ "100" -eq "$PLOSS" ];
then
    echo "$DATE : Starting : $HOST"
    /usr/sbin/pptp pty file /etc/ppp/options.pptp user $PPTPUSER password $PPTPPASS
    echo "$DATE : Now running : $HOST"
else
    echo "$DATE : Already running : $HOST"
fi

Add the following entry to your cron to execute the script every minute.

crontab -e
 */1  * * * * /root/cron/pptp_cron.sh >> /var/log/pptp_pinger.log 2>&1

See my other post if you have not yet set up your PPTP VPN client.


10 Comments

Gary Alan Lee

10-Oct-2014 at 12:36 pm

I have tried this a number of times and for some I can’t get it to work on CentOS 5.11….. What am I doing wrong…? I added a pppd Call at the end of my rc.local config file and the vpn starts fine that way on boot but after about 15 or 20 min that darn system drops the VPN connection….. Bummer…!!!! I get a bunch of command errors when I try to fire-up your sh….

Gary

    james.coyle

    11-Oct-2014 at 11:06 am

    What errors do you get?

Pablo

18-Dec-2014 at 8:56 pm

ppt supports persist and maxtries to retry forever to reconnect. No need of additional script:
http://www.vionblog.com/debian-pptp-client-configuration/

http://linux.die.net/man/8/pppd

Ghosty

19-Feb-2015 at 2:23 pm

Hi,

Thanks for the script! i’ve got a quick qeustion. is it possible to use a command in this script for including the site configuration file stored in /etc/ppp/peers and /etc/ppp/chap-secrets ?

Thanks in advance !

Marcel

Oisín

25-Jul-2015 at 2:22 pm

For the start-on-reboot issue, I think this should suffice:

$ sudo update-rc.d pptpd defaults

Andres Gregori

15-Sep-2015 at 7:13 pm

In my case, I’ve setting cron from /etc/cron.d/pptp-cron file

How ever in order to works I’ve to put this at beggining file:

MAILTO=root
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

*/1 * * * * root /root/cron/pptp_cron.sh >> /var/log/pptp_pinger.log 2>&1

seems to be a normal bug on Debian:
http://serverfault.com/questions/98280/pptp-pon-fails-when-called-via-cron-debian

Nagarjuna Reddy

29-May-2018 at 4:19 pm

Hi i have phased some problems in ppp connection i tried to connection in automatically.
can you please solve that problem . I am using BeagleBoneBlack.

salman.baig

9-Jan-2019 at 10:23 am

can you please tell me is it working on opensuse

Stan

22-Jan-2019 at 10:15 am

connect: Network is unreachable
Tue Jan 22 11:37:01 EET 2019 : Loss Result :
/root/cron/pptp_cron.sh: line 11: [: : integer expression expected
Tue Jan 22 11:37:01 EET 2019 : Already running : 8.8.8.8

line 11 is:
if [ “100” -eq “$PLOSS” ];

why?

Huan

15-Aug-2020 at 12:59 pm

I tried your script and it worked perfectly.

Thank you so much.

Leave a Reply

Visit our advertisers

Quick Poll

Do you use GlusterFS in your workplace?

Visit our advertisers