5

On Ubuntu 14.04, I am using a USB wireless adapter to connect to a wireless network. The connection is often disconnected (I have tried to solve the problem, but I don't know why. That is another question. See the output of dmesg | grep wlan here).

My Network Manager sometimes can automatically reconnect, but sometimes it can't.

So when it disconnects, I often have to run

sudo dhclient -v wlan1

to reconnect. If that doesn't work, I will run

sudo wpa_supplicant -B  -i wlan1 -c /etc/wpa_supplicant.conf
sudo dhclient -v wlan1

If that doesn't work, I will also reload its driver rt2800usb first:

sudo modprobe -r rt2800usb
sudo modprobe rt2800usb
sudo wpa_supplicant -B  -i wlan1 -c /etc/wpa_supplicant.conf
sudo dhclient -v wlan1

How shall we automatically run the commands every time it disconnects, i.e. make re-connection automatic?

There is a problem that mostly the logic name for my USB adapter is wlan1 but sometimes is wlan0.

  • What do you currently have in your /etc/network/interfaces ? For the renaming, you might be able to disable this via the section " freedesktop.org/wiki/Software/systemd/… For the disconnections, that might probably be an issue with the driver, or maybe with your router. See e.g. ubuntuforums.org/showthread.php?t=2242033 My proposal would be to fix the cause of the disconnect/renaming, and not try to workaround the issue by reloading the driver. – doktor5000 May 10 '15 at 17:28
  • auto lo and iface lo inet loopback. The usb adapter's driver only has one parameter nohwcrypt:Disable hardware encryption. (bool). My router works with WPA2 AES PSK, fixed channel 6. – Tim May 10 '15 at 17:49
  • I would like the answer to this as well :( I finally got my Wifi to auto-connect but it only connects @ boot. – FreeSoftwareServers Dec 21 '15 at 9:35
1

You can get the state of wlan1 from /sys/class/net/wlan1/carrier , this is a sample script to check the state of your wifi interface every 2 seconds , then reconnecting ( replace sleep 2 to check the connectivity every n seconds):

while true
     do
     i=$(cat /sys/class/net/wlan1/carrier)
if [ $i == 1 ]
then
       echo "connected"

else
       echo "reconnecting"
       killall wpa_supplicant
       modprobe -rv rt2800usb
       modprobe -v rt2800usb
       wpa_supplicant -i wlan1 -c/etc/wpa_supplicant.conf -B
       dhclient wlan1
       echo "reconnected successfully"
       fi
sleep 2
done

test

Run this script then open a new terminal and run killall wap_supplicant, you will be reconnected again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.