We had a requirement to keep a PPTP VPN Service up 24/7 for one of our servers. We purchased an Online PPTP account that was good enough however, the problem was that PPTP Dialer keeps on disconnecting like once in a day or so. The services relying upon that account were getting disrupted and so we were facing issues.
So I decided to write a startup script in CentOS that could monitor PPTP interface, sense it’s disconnection, dial the VPN again and change the default route to PPP device so that traffic finds a way tunnelled patch to WAN.
For those who are not aware of Linux, you can create a startup service using either of init.d or init (upstart) script. The concept of these two startup scripts is that everytime Linux changes it’s state from one Runlevel to another, it looks for some scripts that are supposed to be run while entering in this Runlevel. The Runlevel is a number ranging from 0 to 6 and declares a particular state of a system.
As stated earlier, your script can either be a traditional init.d script or comparatively newer Upstart Script. Init.d scripts are not smart enough specially when it comes to the scripts that are dependent on each other. Other than that, init.d scripts are complex to be written. On the other hand, the Upstart scripts can be made dependent on each other by defining certain events everytime they start successfully. Besides, they can also made dependent on other events/runlevels or even running services. Another great benefit of upstart script is that it can generate certain subscripts before starting/stopping or after starting/stopping.
So, keeping the advantages in mind, I decided to go ahead with Upstart Script. The script should be capable of 4 tasks automation which are:
- Startup and Halt on Specific Runlevel
- Deleting Old Default Route
- PPTP Dialing
- Adding New Default Route Pointing to PPP Device
First of all, define the Start and Stop Conditions:
start on runlevel  stop on runlevel 
Since we want our script to monitor the VPN Service continuously, we need to make sure it restarts in case it gets killed by any means. For that we need to respawn it and to daemonize it. This can be done by following two statements:
respawn expect fork
Now let’s start the script we need to generate before dialing PPTP VPN. This part is supposed to delete the default route and add a new host via default gateway. In our case default gateway is 10.0.8.1 and Host is 109.x.x.x:
pre-start script /sbin/route del default || true sleep 1s /sbin/route add -host 109.x.x.x gw 10.0.8.1 || true end script
At this point we need to dial our VPN:
exec pppd call ukvpn
Let’s change default static route pointing towards recently connected PPP device using Post Start Script:
post-start script sleep 3s /sbin/route add default dev ppp0 end script
So the overall script becomes:
start on runlevel  stop on runlevel  respawn expect fork pre-start script /sbin/route del default || true sleep 1s /sbin/route add -host 109.x.x.x gw 10.0.8.1 || true end script exec pppd call ukvpn post-start script sleep 3s /sbin/route add default dev ppp0 end script
You need to create a new file using sudo vi /etc/init/pptps.conf and then paste these contents in that file. Once done, you can start and stop service using start pptps, stop pptps. Restart will be like restart pptps and status can be seen using status pptpts.
Drop questions if you need any help.