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