Skip to content
Advertisement

How do I restart wlan0 with the static ip instead of a dynamic ip? [closed]

I have a Raspberry Pi running Raspbian Wheezy. The /etc/network/interfaces is set up to give the Pi a static ip on start up. However, when connection is dropped the Pi won’t re-establish a connection automatically. I have a script that restarts wlan0. However, the Raspberry Pi has a different ip address than the static ip given to it. This breaks the port forwarding I’ve done to access the Pi from outside the network.

It looks as if my interfaces is not set up quite right. The Pi can be accessed from two ip addresses within the network, one is the static address I defined while the other is not. When wlan0 is restarted, a dynamic ip address is given to the Pi, but not the static address.

Here is my /etc/network/interfaces:

     auto lo
     iface lo inet loopback

     auto eth0
     allow-hotplug eth0
     iface eth0 inet manual

     auto wlan0
     allow-hotplug wlan0
     iface wlan0 inet static
     address 192.168.1.11
     netmask 255.255.255.0
     gateway 192.168.1.1
     wpa-ssid "ROUTER NAME"
     wpa-psk "PASSWORD"

     auto wlan1
     allow-hotplug wlan1
     iface wlan1 inet manual
     wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

Here is the script I’m using to reset wlan0:

     #!/bin/bash

     SERVER=192.168.1.1 #Ping the router

     ping -c2 ${SERVER} > /dev/null

     #If the exits status from the ping is not 0 (failed)
     if [$? != 0]
     then
         #Disable wlan0 and re-enable it
         sleep 2
         ifconfig wlan0 down 
         sleep 2
         ifconfig wlan0 up
     fi

Any help is appreciated! Thanks!

Edit: After looking around with ideas found in this thread, I found /etc/init.d/networking restart does everything I need.So the new script is

    #!/bin/bash

    SERVER=192.168.1.1
    ping -c2 ${SERVER} > /dev/null

    if [ $?!=0 ]
    then
        /etc/init.d/networking restart
        echo "Reconnecting!"
    fi

Advertisement

Answer

I looked around a bit and it seems you may be able to make sure it is set in your wifi script after ifconfig wlan0 up (I’ve also had to implement such a script on every raspberry pi setup I’ve ever made due to wifi inconsistencies)

if [$? != 0]
     then
         #Disable wlan0 and re-enable it
         sleep 2
         ifconfig wlan0 down 
         sleep 2
         ifconfig wlan0 up
         ifconfig wlan0 192.168.1.11
     fi

Though it should be noted that I personally have never set up a static ip on a pi, I use a reverse tunneling service so I never have to worry about port forwarding. My answer references this answer from the raspberrypi stackexchange site. https://raspberrypi.stackexchange.com/questions/9678/static-ip-failing-for-wlan0

Let me know if that helps otherwise I can dig deeper into it.

According to feedback from the asker, the /etc/init.d/networking restart was the answer, read comments below.

Advertisement