This post is second part of Configuring VPN client. After completion steps in previous post, VPN will work with starting pppd and manually adding route. With a few PHP lines and configuration of ip-up.local file, VPN connection can be automated and reduced to only one script.
And it can look like:
# start a VPN connection vpn.php start # stop a VPN connection vpn.php stop
I choose PHP for scripting, but it can be Bash or Perl as well. Please save PHP file as vpn.php and set permissions to 755.
#! /usr/bin/php
<?
// define usage message
$usage = 'Usage: ' . basename($argv[0]) . " {start|stop|}\n";
// test number of input parameters (first parameter is script itself)
if ($argc != 2) exit($usage);
// second parameter should be 'start' or 'stop'
if ($argv[1] != 'start' && $argv[1] != 'stop') exit($usage);
// start a VPN connection (route is defined in /etc/ppp/ip-up.local)
if ($argv[1] == 'start')
system('cd /etc/ppp/peers/; /usr/share/doc/ppp-2.4.4/scripts/pon my_vpn');
else // or stop a VPN connection
system('/usr/share/doc/ppp-2.4.4/scripts/poff -a');
?>
Final step – create /etc/ppp/ip-up.local file with the following content:
#!/bin/bash /sbin/route add -net x2.y2.w2.0 netmask 255.255.255.0 dev ppp0
This script is executed right after pppd starts. Don’t forget to set execute permissions or route will not be added. I also experimented with “route add” directly in the PHP script, and the result was: SIOCADDRT: No such device. I suppose that “route add” tried to start before pppd initialization finished. After “route add” was placed to the /etc/ppp/ip-up.local file, error has disappeared. VPN and route were correctly started.