Ubuntu Disconnecting from Wi-Fi and Failing to Reconnect.

I have an updated fix for this issue using a startup script to connect and a time scheduled script for checking the connection, rather than having one constantly running in the background, avalible here – http://alexsleat.co.uk/2011/01/09/a-more-elegant-solution-to-ubuntu-wi-fi-reconnecting-issue/

I’ve been having some trouble with my wifi on Ubuntu recently but I think it’s because I’m almost out of range of the access point. The problem is that once it disconnects it never seems to be able to reconnect unless wireless is disabled and re-enabled. No doubt there is another way of fixing this issue but it gives me a chance to write my first shell script.

In short the script disables network-manager (it was trying to do things auto which was screwing with things), sets up the connection and connects then checks for a string in the wireless card parameters which will only occur if it’s disconnected, if the string appears it’ll disable and re-enable the wireless device which seems to allow it to connect again. If it’s connected it’ll wait 3 minutes (180 seconds) before checking again where as if it does disconnect it’ll check again 30 seconds after it’s tired rebooting the device.

Here’s the script, be sure to try it if you’re having the same problem (you might need to change the wireless device ‘wlan1’ to yours) and comment if I’m doing something wrong or there’s another way around this, cheers.

#!/bin/bash

service network-manager stop

iwconfig wlan1 essid NETWORKNAME
iwconfig wlan1 key NETWORKKEY
ifconfig wlan1 up
dhclient3 wlan1

while true;
do

if (iwconfig wlan1) | grep -o "Access Point: Not-Associated"
then
	ifconfig wlan1 down
	echo "not connected, rebooting Wifi"
	ifconfig wlan1 up
	sleep 30
else
	echo "connected"
	sleep 180
fi

done

4 comments to Ubuntu Disconnecting from Wi-Fi and Failing to Reconnect.

  • Brian

    This made my day so much easier, thanks

  • Glad to hear Brian! I recently found out about cron too, if you don’t already know about it – it basically schedules scripts or commands to be run hourly, daily, weekly etc (I believe it also allows them to be run more frequently but I’m still looking into that) by putting the script into the directory (e.g. /etc/cron.hourly) which can reduce the script size and stop it constantly running in the background, I’ll make a new post on the edited script and the whole cron thing in the next few days and link to it.

  • […] 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 […]

  • […] Here is a good tutorial show you about Ubuntu Disconnecting from Wi-Fi and Failing to Reconnect: I’ve been having some trouble with my wifi on Ubuntu recently but I think it’s because I’m almost out of range of the access point. The problem is that once it disconnects it never seems to be able to reconnect unless wireless is disabled and re-enabled. No doubt there is another way of fixing this issue but it gives me a chance to write my first shell script. […]

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>