Auto reconnect to wifi once disconnected?

10,916

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

testing the script

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

Share:
10,916

Related videos on Youtube

Tim
Author by

Tim

Elitists are oppressive, anti-intellectual, ultra-conservative, and cancerous to the society, environment, and humanity. Please help make Stack Exchange a better place. Expose elite supremacy, elitist brutality, and moderation injustice to https://stackoverflow.com/contact (complicit community managers), in comments, to meta, outside Stack Exchange, and by legal actions. Push back and don't let them normalize their behaviors. Changes always happen from the bottom up. Thank you very much! Just a curious self learner. Almost always upvote replies. Thanks for enlightenment! Meanwhile, Corruption and abuses have been rampantly coming from elitists. Supportive comments have been removed and attacks are kept to control the direction of discourse. Outright vicious comments have been removed only to conceal atrocities. Systematic discrimination has been made into policies. Countless users have been harassed, persecuted, and suffocated. Q&A sites are for everyone to learn and grow, not for elitists to indulge abusive oppression, and cover up for each other. https://softwareengineering.stackexchange.com/posts/419086/revisions https://math.meta.stackexchange.com/q/32539/ (https://i.stack.imgur.com/4knYh.png) and https://math.meta.stackexchange.com/q/32548/ (https://i.stack.imgur.com/9gaZ2.png) https://meta.stackexchange.com/posts/353417/timeline (The moderators defended continuous harassment comments showing no reading and understanding of my post) https://cs.stackexchange.com/posts/125651/timeline (a PLT academic had trouble with the books I am reading and disparaged my self learning posts, and a moderator with long abusive history added more insults.) https://stackoverflow.com/posts/61679659/revisions (homework libels) Much more that have happened.

Updated on September 18, 2022

Comments

  • Tim
    Tim over 1 year

    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.

    • Tim
      Tim about 9 years
      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.
    • FreeSoftwareServers
      FreeSoftwareServers over 8 years
      I would like the answer to this as well :( I finally got my Wifi to auto-connect but it only connects @ boot.
  • JacKeown
    JacKeown over 3 years
    Do you mean wpa_supplicant?