1

after connecting to a network via wpa_supplicant I need a way to know if we are disconnected so we can connect again automatically.

user12448
  • 177
  • 1
  • 2
  • 9

1 Answers1

0

I've copied and modified a script for this (the copyright-claims are the originals) :

I did some modifications, the one I can remember is adding the silence option. place this script in /home/pi/.wifi_check, add execution rights (chmod a+x /home/pi/.wifi_check) and add this line to /etc/crontab/ to let it test the connection every 5 minutes:
*/5 * * * * root /home/pi/.wifi_check silent

#!/bin/bash
##################################################################
# A Project of TNET Services, Inc
# Original verion can be found at:
# https://github.com/dweeber/WiFi_Check
# This version is modified by:
# ExploWare  http://raspberrypi.stackexchange.com/users/13296/exploware
#
# Purpose:
#
# Script checks to see if WiFi has a network IP and if not
# restart WiFi
#
# Uses a lock file which prevents the script from running more
# than one at a time.  If lockfile is old, it removes it
#
#
##################################################################
##################################################################
# Settings
# Where and what you want to call the Lockfile
lockfile='/run/lock/WiFi_Check.pid'
# Which Interface do you want to check/fix
wlan='wlan0'
wired='eth0'
silent=false
if [[ "$1" == "silent" ]]; then
    silent=true
fi
##################################################################
#echo
$silent || echo -n "Starting WiFi check for $wlan"
#echo 

# Check to see if there is a lock file
if [ -e $lockfile ]; then
    # A lockfile exists... Lets check to see if it is still valid
    pid=`cat $lockfile`
    if kill -0 &>1 > /dev/null $pid; then
        # Still Valid... lets let it be...
        #echo "Process still running, Lockfile valid"
        $silent || echo -n .
        exit 1
    else
        # Old Lockfile, Remove it
        #echo "Old lockfile, Removing Lockfile"
        $silent || echo -n .
        rm $lockfile
        sleep .5
    fi
else
    $silent || echo -n .
fi
# If we get here, set a lock file using our current PID#
#echo "Setting Lockfile"
$silent || echo -n .
echo $$ > $lockfile

# We can perform check
#echo "Performing Network check for $wlan"
if iwconfig $wlan|grep -qe "ESSID:\".*\""; then
    #echo ESSID found
    $silent || echo -n .
    if ifconfig $wlan | grep -q "inet addr:" ; then
        #echo "Network is Okay"
        $silent || echo . OK
        logger Wifi connection on $wlan is OK
        ifdown $wired 2&>1 > /dev/null 
    elif ifconfig $wlan | grep -q "inet6 addr:" ; then
        #echo "Network \(IPv6 only\) is Okay"
        $silent || echo . OK
        logger Wifi connection on $wlan \(IPv6 only\) is OK
        ifdown $wired 2&>1 > /dev/null 
    else
        $silent || echo "Network connection down! Attempting reconnection."
        logger Wifi connection on $wlan is DOWN, trying to reconnect... 
        date
        ifdown $wlan
        sleep 5
        ifup --force $wlan
        if ifconfig $wlan | grep -q "inet addr:" ; then
            logger Wifi connection on $wlan seems to be up and running again.
            ifdown $wired 2&>1 > /dev/null 
        else
            logger Wifi connection on $wlan is DOWN, enabling the wired connection...
            ifup $wired 2&>1 > /dev/null 
        fi
    fi
else
    $silent || echo "Network connection down! Attempting reconnection."
    logger Wifi connection on $wlan is DOWN, trying to reconnect... 
    date
    ifdown $wlan
    sleep 5
    ifup --force $wlan
    if ifconfig $wlan | grep -q "inet addr:" ; then
        logger Wifi connection on $wlan seems to be up and running again.
        ifdown $wired 2&>1 > /dev/null 
    else
        logger Wifi connection on $wlan is DOWN, enabling the wired connection...
        ifup $wired 2&>1 > /dev/null 
    fi
fi

$silent || echo -n "Current Setting:"
$silent || iwconfig $wlan | grep -oe "ESSID:\".*\"";ifconfig $wlan | grep "inet addr:"

# Check is complete, Remove Lock file and exit
#echo "process is complete, removing lockfile"
rm $lockfile

##################################################################
# End of Script
##################################################################
ExploWare
  • 350
  • 1
  • 10
  • This is a solution to your problem, but the exact answer to your question is: see if a network address is assigned to your interface (ifconfig $wlan | grep -q "inet addr:") – ExploWare Mar 18 '14 at 21:32
  • what about this solution ls /sys/class/net/wlan0/operstate – user12448 Mar 18 '14 at 21:40
  • @user12448 My interface can be up, searching for the accesspoint, while not being connected. There could be a flaw in this script, it could be only working with DHCP, havent tested in static-configuration – ExploWare Mar 18 '14 at 21:43
  • your solution is good but how to make it work in ubuntu there is no file called wifi_check pid (i start always testing – user12448 Mar 18 '14 at 21:46
  • the pid file is only in its place when this script is running. It contains the ProcessID of this script. That way it can make sure there is only one instance running, so you cannot accidentally start it multiple times – ExploWare Mar 18 '14 at 21:49
  • the script is wrong – user12448 Mar 18 '14 at 21:51
  • I'm connected the network and it shows me network down -__- – user12448 Mar 18 '14 at 21:51
  • I will see it in deep tomorrow and give you the review – user12448 Mar 18 '14 at 21:51
  • have you set the correct parameters? Is your wlan-interface called wlan0 and is your wired network 'eth0'? – ExploWare Mar 18 '14 at 21:53
  • yes they have the same names as yours – user12448 Mar 18 '14 at 21:55
  • I can connect to the access point but I don't have an IP ! but good try ! – user12448 Mar 18 '14 at 21:56
  • True, this test is only suitable for IPv4 addresses. I'll add a line for IPv6 as well, but it has to be IPbased networking, and for me (as I guess most users) that'll be sufficient. – ExploWare Mar 18 '14 at 22:01
  • you didn't catch what i mean ! for example you can be connected to an access point but still not have an ip (your script will show me not connected if it is correct of course ) – user12448 Mar 18 '14 at 22:07
  • Sorry, maybe I'm not getting you: How can your RaspberryPi be connected using DHCP to a AccessPoint correctly and not have an IPaddress? – ExploWare Mar 18 '14 at 22:09
  • without dhcp server I mean ! your example didn't treat this case – user12448 Mar 18 '14 at 22:12
  • Ok, as I state in the 3rd comment: There could be a flaw in this script, it could be only working with DHCP, havent tested in static-configuration. But when you set a correct /etc/network/interfaces conifguration, you would have to set an address manually, so it will have an address, right? – ExploWare Mar 18 '14 at 22:13
  • yes after assigning it will not show "inet addr" but again your script didn't work – user12448 Mar 18 '14 at 22:17
  • the idea is good keep thinking of a nice one my second comment could give you a hint – user12448 Mar 18 '14 at 22:18
  • I have updated the script, ignoring your suggestion ;) because of the fact: a device can be up, but not connected to an accesspoint. In this same case it can have a static IP, while not connected. So, the updated code checks for an ESSID in the iwconfig, which only returns True if its successfully connected. I hope its of use for you, or anyone for that matter. It sure is for me... – ExploWare Mar 18 '14 at 23:40