Script to Automatically Detect and Restart Linux PPTP Client
Written by Rudy
Thursday, 22 September 2016 07:16

The 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.
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#!/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.
|
*/1 * * * * /root/cron/pptp_cron.sh >> /var/log/pptp_pinger.log 2>&1
|