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 !
wlan=`/sbin/ifconfig wlan0 | grep inet\ addr | wc -l`
if [ $wlan -eq 0 ]; then
/sbin/ifdown wlan0 && /sbin/ifup wlan0
echo interface is up
I’ve setup this script to run as a cronjob every 5 minutes to make sure the WiFi stays up.
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 🙂
No probs. Glad it helped
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.