# cron job script needed to keep pptp link up [solved]

## Majed17

hello, i want my pptp connection to stay up at all times so,

i tried adding persist to the tunnel like so :

```

pty "pptp ip adress --nolaunchpppd"

name domain\\username

remotename PPTP

require-mppe-128

file /etc/ppp/options.pptp

ipparam tunnel name

persist
```

but it didn't work, the server was unavailable for hours and it didn't connect when it came back online.

i searched the internet for a script to check if pptp is up and if down to bring it up again but i only found one that adds routes but didn't see any pon in it.

so what i need is a script to check if the tunnel is down and if it is, to do a cron job to poff then pon the tunnel up again.

has anyone come across this? any help is appreciated  :Smile: Last edited by Majed17 on Thu Nov 03, 2011 9:25 am; edited 1 time in total

----------

## plindbe2

If when the tunnel goes down, the interface is not listed by ifconfig, make a script that runs ifconfig and checks to see if the interface is listed.  If it is, then do nothing otherwise try to bring it up.  Then add this script to the cron daemon to run every minute by using wildcards in the crontab file.

I leave this as an exercise.   :Wink: 

----------

## Majed17

well thanks for the idea, although i've not done any cron job before but i think i could manage it and findout how to do it but i am no programer and doing a script is still out of my reach, i'm currently searching the internet for something similar and hopefully i'll find one.

i hope you are not one of those who enjoys causing suffering to people..

so far i found out a script which said:

#!/bin/sh

#echo ${PPP_IPPARAM}

#if [ "${PPP_IPPARAM}" = "vpn" ]; then

route add -net 192.168.157.0/8 dev ${IFNAME}

#fi

so know i know how the script should start with and end with but no idea how to do it  :Smile: .

----------

## plindbe2

Hello,

What I posted earlier was just an idea that I thought you might be able to go on, nothing less than friendly.  Anyways, here is how I'd go about the script (using perl).  Now this is assuming that an interface called ppp0 exists when the pptp connection is active and does not exist when it is inactive (important).

```
#!/usr/bin/perl

#Author: plindbe2

#Run ifconfig and put into array @ifconfig

open(OTPT, "ifconfig|");

chomp(@ifconfig = <OTPT>);

close(OTPT);

$switch = 0;

foreach $line (@ifconfig){

   #if the line begins with ppp0 in the ifconfig command, switch = 1

   if($line =~ m/^ppp0/){

      $switch = 1;

   }

}

unless($switch){

   print "Bringing up the interface\n";

   #put whatever your command is to start your pptp connection inside the double quotes

   system("pon \$TUNNEL");

}
```

Now as for cron.  Please read http://www.gentoo.org/doc/en/cron-guide.xml.

I recommend vixie-cron.

Edit the crontab by running 

```
crontab -e
```

as root or through sudo/su/gksu/...

Remember to cycle the cron-daemon after crontab is modified.

```
/etc/init.d/vixie-cron restart
```

I hope this helps!

----------

## Majed17

well thank you very much for the script, i have yet to try it.

i had very hard time with crontab,

everytime i did 

```
crontab -e
```

i got the error:

```
/bin/sh: /bin/nano: No such file or directory
```

after some thinking  :Smile:  i resolved it with 

```
emerge nano
```

  :Smile: 

before that i was editing the crontab file with an external editor which seems to be the same.

anyway for a test i tried the following in a test.cron file:

```
*     *     *      *     *       /bin/echo "test" >/var/test
```

which is a job to write test to the file test in /var every minute. followed by 

```
crontab test.cron
```

 and then  

```
/etc/init.d/vixie-cron restart
```

 I was expecting the file to be filled with "test" in many lines except i got one line with test so i thought it only performed the job once but then i cleared the "test" word and sure enough after a minute it reappeared which mean the cron job is working. yet i am still bedazzled on why it replaced the word test everytime instead of adding it to a new line.

latsly, i use the command:

```
pon profiland
```

to connect the ppp0 device. So i assume that the script should look like this:

```
.

.

.

unless($switch){ 

   print "Bringing up the interface\n"; 

   #put whatever your command is to start your pptp connection inside the double quotes 

   system("pon profiland"); 

}
```

or should i keep the slash. P.S. your logic is correct ppp0 doesn't exist when the pptp is off.

except the echo command in the script will not appear on the screen because according to the link you gave me 

"Note: You won't actually see the output from these echo commands unless you use redirection. "

and i have no idea what they mean by redirection

----------

## plindbe2

the reason you're not seeing a lot of "test" lines and just one is because of your command

```
/bin/echo "test" >/var/test
```

which prints test and sends the output to a new file each and every time it is executed.  If you want to concatonate onto each line "test" then use the double greater than symbol like such

```
/bin/echo "test" >> /var/test
```

The cron file looks good, and as for the printing of "Bringing up the interface".  You can just comment out or delete that line.  It will not be visible to any console because the text is not redirected.  It is useless.  

Lastly, if your command to turn on your pptp tunnel is

```
pon profiland
```

Then edit the system line on the script to:

```
system("pon profiland");
```

I simply escaped the dollar sign because you have to do that with dollar signs and double quotes otherwise perl will think that it's a variable.

----------

## Majed17

thanks for clearing the issue on writing to logs in cron.

i set up the cron job but i forgot to make the cron job exutable so it didn't work out. i had to travel to the remote site and make it executable but when i arrived the server couldn't connect through putty so i had to shutdown it by the the power button. then when it powered up i tried 

```
#perl /var/script.pl
```

and the out put was 

```
bringing up the interface
```

and then in if config ppp0 appeared so it succeeded.

next i added it to the cron job as follows:

```
*     *     *     *     *        perl /var/script.pl
```

in the file test.cron located also in /var

i noticed vixie cron was stopped and so i started it. i connected the gentoo directly to a monitor to see the gentoo message and it was hung on getting time from ntp-client so i removed ntp-client for startup by:

```
rc-update del ntp-client default
```

after some reboots vixie-cron worked but at othertimes i couldn't restart vixie-cron it would say:

```
vixie-cron failed to start
```

and something about process-daemon.

after a reboot it indicated that vixie-cron started but i didn't try anymore to restart it.

anyway it connected the pptp tunnel and i could ping but i noticed that in the output of ifconfig there was 

```
ppp1 and ppp2
```

so i think since ppp0 for some reason got disconnected it kept adding on connections like ppp2 and ppp3. it might keep doing this indefinitely.

to my bad luck when i left and came back to the office there was no ping to the gentoo machine. i think the bug in the script is that it checks if ppp0 exists and if not it executes "pon profiland". i think it should check if "ppp" exists in general and and if not then to "pon profiland" and to check if ppp1 or 2 exists and then turn off the tunnels so that all ppp's disappear by executing "poff profiland" and after that reconnect the tunnel by "pon profiland" if there are no ppp devices present.

i feel i have already asked for too much but what can i do the problem still exists:) i hope you be patient and try to help me out, i am sure others will also benifit from this topic too when it's all working out. thanks for your great help again  :Very Happy:  much appreciated  :Smile: 

----------

## Majed17

hello, today we tried to open a dhcp pool for vpn connections on the server and managed to connect to the gentoo machine for a minute before it became unavialable again. we ran 

```
ifconfig
```

and there were 24 ppp connections hanging, i.e up to ppp23.

it seems the switch = 1 is not working properly, the cron script is executing the script every minute and every minute it brings up a new ppp connection.

i'm going to try to replace switch = 1 by print "interface is up\n"

with no switch = 1 and see what happens.

----------

## Majed17

i added 

```
print "interface is up\n"; 
```

after switch = 1

when running perl script.pl i get interface is up when ppp0 is up. but in the cron job the output is always bringing interface up which is why i am getting tens of ppp connections.

i searched the internet for a solution and they said to include the full path which would look like (in the cron job)

```
*     *     *     *     *        /usr/bin/perl /var/script.pl
```

but it didn't work. then i found a solution to make the perl script run in a /bin/sh enviroenment but i didn't manage to know how to do it, maybe i'll look for it tomorrow.

finally i found /bi/sh loop script which would ping an address or ip and bring the ppp interface up if the ping times out (no ping).

then i modified the script (it was easier to learn in this shell than in perl  :Smile:  )

i made the script from a loop to a normal script which pinged and waited if ping succeeded and then if it didn't succeed it would bring the interface up and wait 4 seconds before bringing it up again.

here is the content of the script:

```

#!/bin/sh

# Script for /usr/bin/internetd to check for internet connection and if not present,

# execute the script in variable bring_interface_up

website_to_ping="10.1.1.160"

bring_interface_up="pon plg"

disconnect="poff plg"

wait="1"

wait2="4"

seconds_to_sleep="60"

number_of_times_to_ping="3"

#do not change the infinite_loop_value variable/constant

#infinite_loop_value="1"

#while [ "$infinite_loop_value" = "1" ];

#do

ping -c "$number_of_times_to_ping" "$website_to_ping"

if [ "$?" = "0" ];

#do

then

#nothing, but wait and check again

sleep "$seconds_to_sleep"

ping -c "$number_of_times_to_ping" "$website_to_ping" >> "/var/test"

#done

else

#if loop is broken, execute script to bring interface up

echo disconnected ;>> "/var/test"

sleep "$wait"

poff plg ;>> "/var/test"

sleep "$wait"

echo diconnecting if connected... ;>> "/var/test" 

sleep "$wait"

echo waiting... ;>> "/var/test"

sleep "$wait2"

pon plg ;>> "/var/test"

sleep "$wait"

echo connected. ;>> "/var/test"

echo Interface Brought Up: $(date) >> "/var/test"

fi

#done
```

but then as always i had trouble with cron. it echoed the commands in to /var/test when needed but didn't execute pon or poff.

i searched the internet and found that the variable paths must be included.

the cron job was as follows:

```
*     *     *     *     *        /var/loop.sh
```

i changed it into :

```

SHELL=/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin

*     *     *     *     *        /var/loop.sh

```

and now it works in cron too  :Smile: 

but i'm still kinda interested why perl didn't work yet  :Smile: 

any idea?

----------

