Jan 102013
 

A while ago, I was having an issue with one of my computer’s WiFi connection not connecting correctly on startup.
Maybe the signal was too weak on startup or the WiFi adapter just wasn’t fast enough when the computer wanted it to connect, for some reason it would never connect and I would have to manually run an ifup command to make it connect.

I ended up making this script to check whether the WiFi had an IP address, and if it didn’t it would take down the WiFi adapter and bring it back up again.
Now, I never need to worry about that computer’s WiFi connection !
I was also going to use this for one of my Raspberry Pi projects but never got around to putting the script onto the Pi, so anyone who has the issue where hostapd breaks the IP address, this script is perfect for it !


#!/bin/bash

wlan=`/sbin/ifconfig wlan0 | grep inet\ addr | wc -l`
if [ $wlan -eq 0 ]; then
/sbin/ifdown wlan0 && /sbin/ifup wlan0
else
echo interface is up
fi

I’ve setup this script to run as a cronjob every 5 minutes to make sure the WiFi stays up.

Share

  4 Responses to “Script : WiFi Checker Script”

  1. I have two Rpi’s with micro wifi usb adapters. They are not so stable when put far from access point, sometimes lost connection and don’t reconnect.
    Thanks for your script, it helped me a lot 🙂

  2. You just saved me 5 minutes of BASH scripting 🙂

    Swapped out the ifdown and such for code to turn an LED on or off (connected to a Raspberry Pi GPIO pin) and now I have an LED indicator for IP address.

Leave a Reply to Tim B Cancel reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

This site uses Akismet to reduce spam. Learn how your comment data is processed.