after connecting to a network via wpa_supplicant I need a way to know if we are disconnected so we can connect again automatically.
Asked
Active
Viewed 1,800 times
1 Answers
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
ifconfig $wlan | grep -q "inet addr:"
) – ExploWare Mar 18 '14 at 21:32wlan0
and is your wired network 'eth0'? – ExploWare Mar 18 '14 at 21:53/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:13iwconfig
, 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