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?

user151324
  • 1,270
  • 4
  • 14
  • 17
user7848
  • 81
  • 1
  • 1
  • 2

4 Answers4

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
Jivings
  • 22,656
  • 11
  • 94
  • 140
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

lenik
  • 11,533
  • 2
  • 32
  • 37
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.

Piotr Kula
  • 17,336
  • 6
  • 66
  • 105
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/

KayEss
  • 346
  • 1
  • 6