Categories
Blog Command Line Linux Posts Ubuntu

A More Elegant Solution to Ubuntu Wi-Fi Reconnecting Issue

Previously I was having problems with Ubuntu dropping wifi connections and failing to reconnect, to solve this I wrote a script which would kill the network-manager and then connect using iwconfig commands (here). While this works fine, it felt a little hacky, having to have a script running as sudo constantly in the background checking for a dropped connection. After a little searching I came across cron (the time based job scheduler ) and /etc/rc.local (a script which is run after all other initialization scripts have ran, allowing for scripts to be ran on startup) so from this I decided to split the old script up into connection and checking scripts which could be ran from init.d/local and cron, respectively.

Firstly my startup script in /etc/init.d/local which sets up and connects to the wireless network, open the file as sudo:

 sudo gedit /etc/rc.local

Paste in the following:

#! /bin/sh

service network-manager stop && service networking stop

iwconfig wlan0 essid NETWORKNAME
iwconfig wlan0 key WEPKEY
ifconfig wlan0 up
dhclient3 wlan0

sleep 10

if iwconfig wlan0 | grep -o "Access Point: Not-Associated"
then
	ifconfig wlan0 down
	sleep 10
	ifconfig wlan0 up

fi

exit 0

Next the script for checking the network is still connected, if not attempt to reconnect (named wirelesscheck.sh):

#!/bin/bash

if iwconfig wlan0 | grep -o "Access Point: Not-Associated"
then

	ifconfig wlan0 down
	sleep 10
	ifconfig wlan0 up
	
fi

Make sure this script is executable (from the directory of the script):

 chmod +x wirelesscheck.sh 

Note: This is setup in the sudo crontab, only because this command needs root privileges – other commands could be added to a user crontab (by removing sudo from the following.)

Now, edit (-e) the crontab for sudo:

 sudo crontab -e 

If crontab has not previously been used choose an editor (I used nano – 2) and append this line to the bottom of the file and change the frequency and directory of the script. The current settings will run it every 5 minutes (*/5) every hour, day, month and year and the file is located in “/path/to/script/wirelesscheck.sh”.

*/5 * * * * /path/to/script/wirelesscheck.sh

If you have any issues, leave a comment and I will help if I can.

8 replies on “A More Elegant Solution to Ubuntu Wi-Fi Reconnecting Issue”

Hi There:

In some cases (like me) the wifi appears still connected… so i change the script a little, hope helps!

#!/bin/bash
if ping -c 2 192.168.2.254 | grep -o “Destination Host Unreachable”
then
ifconfig wlan0 down
sleep 5
iwconfig wlan0 essid NAMEHERE
iwconfig wlan0 key KEYHERE
ifconfig wlan0 up
fi

After a few weeks of preparing to throw my laptop from the top of my building, I believe your scripts have saved it’s fate. Thank you! This disconnect with no reconnect issue was driving me nuts. thank you for the simple fix, albeit a workaround to a problem inherent in the system itself.

If you use the ifup/ifdown scripts and you configured your /etc/network/interfaces correctly there is no need to use the ssid, just the interface. I also modified it slightly to lookup the default gw automatically, and ping that, so if your mobile it will use whichever is the active default gateway.

If you use the ifup/ifdown scripts and you configured your /etc/network/interfaces correctly there is no need to use the ssid, just the interface. I also modified it slightly to lookup the default gw automatically, and ping that, so if your mobile it will use whichever is the active default gateway.

#!/bin/bash
GW=`/sbin/ip route list | grep default | awk ‘{print $3}’`
if ping -c 2 $GW | grep -o Unreachable; then
echo down;
echo `date`”: reconnected to wlan” >> /var/log/wlan0-detail
ifdown wlan0
sleep 5
ifup wlan0
fi

this is exactly what I was looking for. after doing the mentioned steps I rebooted, it doesn’t connect to my wireless network. I tried the 1st command
iwconfig wlan0 essid NETWORKNAME in terminal and I get:
set failed on device wlan0 operation not permitted
then I checked: iwconfig wlan0 and I got: no such device.
any ideas what am I doing wrong?

thank you.

You need to replace wlan0 with whatever your device is named. Use the command ‘ifconfig’ to return a list of network devices and replace every instance of it in the script and instructions to the device you wish to connect.

Leave a Reply

Your email address will not be published. Required fields are marked *