8

I want to use the Raspberry Pi as a VPN gateway. The RPi connects to my home network and the internet via wlan0, and any device plugged into eth0 gets a VPN connection. I am using pptp-linux to make the VPN connection and everything worked fine during my prototyping stage. However, to complete the project I have put the VPN connection in the /etc/rc.local script so that it will start at boot. Now it fails 50% of the time. I suspect that it is attempting the VPN connection before I am properly connected to the internet via wlan0. If this is the problem, I need a test for a valid internet connection at the beginning of rc.local before proceeding with the rest of the script. Any ideas as to how I could do this?

  • You could add a sleep 20 before your script. Or better, put your script in /etc/network/if-up.d. That way the script is run once the network interface goes up. – Gerben Jun 8 '13 at 20:15
7

From this StackOverflow answer;

Ping your local gateway;

#!/bin/bash
ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` > /dev/null && echo ok || echo error
| improve this answer | |
4

right off the top of my head:

  1. ping your router

  2. ping google.com

  3. grep ifconfig output for valid gateway and/or valid IP address

| improve this answer | |
  • You would need to grep the pings for a valid reply. In a while loop until all conditions are met. Just pinging wont do much. Jivings quoted a good line of code. – Piotr Kula Jun 10 '13 at 16:05
  • @ppumkin no need to use grep, ping uses exit codes to tell you about the results, something like ping -qc 1 192.168.1.1 && echo ok || echo error – lenik Jun 10 '13 at 22:26
4

Based on Jivings sample code and some of my own logic.

#!/bin/bash

STATE="error";

while [  $STATE == "error" ]; do
    #do a ping and check that its not a default message or change to grep for something else
    STATE=$(ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` > /dev/null && echo ok || echo error)

    #sleep for 2 seconds and try again
    sleep 2
 done

#put your VPN code here...

The proper way would be to create a function that returns a boolean on the while comparator. but this is how the shell logic should look like.

| improve this answer | |
1

Debian now uses upstart. This is probably a much simpler way of doing it that init.d scripts.

You may need to try a few different services to wait on, but networking (if it's there) or network-manager might be a good ones to try first.

This is for Ubuntu, but you shouldn't find too many differences http://upstart.ubuntu.com/cookbook/

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.