How to Auto Reconnect PPTP in CentOS/Redhat

Written by BiRU Thursday, 22 September 2016 07:17

Print

All of us may have seen  that in windows based OS, PPTP connection always make attempt to reconnect PPTP whenever any kind of termination occur in connection. Have you think about how this will work in CentOS or Redhat machine?

Definitely we will seek help from CRON, but here is a little bit trick. Let’s see…

First of all, we need to set a PPTP connection in /etc/ppp/peers/ like this:

[root@localhost ~]# vim /etc/ppp/peers/new_pptp

pty “pptp 10.12.10.2 –nolaunchpppd”
name USERNAME
password secret_password
remotename PPTP
require-mppe-128

Now we need to make a script to call pptp as follows:

[root@localhost ~]# vim /etc/ppp/peers/auto_dial.sh

#!/bin/bash
if ! ping -Q 2 -c 2 -t 2 192.168.6.1; then
/usr/sbin/pppd call new_pptp
sleep 5
/sbin/route add default ppp0
fi

Explaination:

  1. if ! ping -Q 2 -c 2 -t 2 192.168.6.1; then : The condition is set such as the commands in the script will work only when this condition full-fill. Here 192.168.6.1 will be the gateway IP of PPTP ip block. Which means it is the gateway of the network on which client will be connected. This should be placed at PPTP server interface. So, the script will check 2 times  ping result. If ping drop occurs in consecutive 2 times, it will run next commands.
  2. /usr/sbin/pppd call new_pptp: This is to call PPTP connection.
  3. sleep 5: Wait for 5sec before moving to next command. This interval is given to establish PPTP connection.
  4. /sbin/route add default ppp0: This will add default route via PPTP interface.

Here comes the final part of this work. Let’s make a CRON job to run this script. Open crntab file using nano and add with your required interval. Here I’ve instructed CRON to run this at every 2 minute interval.

[root@localhost ~]# nano /etc/crontab

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

# For details see man 4 crontabs

# Example of job definition:
# .—————- minute (0 – 59)
# | .————- hour (0 – 23)
# | | .———- day of month (1 – 31)
# | | | .——- month (1 – 12) OR jan,feb,mar,apr …
# | | | | .—- day of week (0 – 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
*/2 * * * * root /etc/ppp/peers/auto_dial.sh

Now start the cron using /etc/init.d/cron start