LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Returning wifi and dhclient after suspend (https://www.linuxquestions.org/questions/linux-networking-3/returning-wifi-and-dhclient-after-suspend-4175552061/)

ario 08-29-2015 04:46 AM

Returning wifi and dhclient after suspend
 
One of the problems I got with all of the Linux distro's was network manager software in them. Sometimes network manager just can't get you connected to the same wifi after you suspended your laptop and all you can see is a turning circle. So I wrote a shell script which I tried and is very successful in getting things done on wifi. If you're experiencing similar problem, reading through that can give you a hint how to solve your problem as well.
First of all I do everything manually because I'm tired of Network Managers at the time. So I developed a shell script for creating new wifi connections:
Code:

#!/bin/bash
if [ $# -lt 2 ]; then
        echo "syntax: wifi-new <ssid> <passphrase>"
        exit 1
fi
mkdir -p /etc/wpa_supplicant
wpa_passphrase $1 $2 > /etc/wpa_supplicant/$1.conf
echo "/root/wifi-connect $1.conf" > "/root/wifi-connect-"$1

So this script creates a wpa_supplicant config file in some folder in /etc. This is what network manager applets show you as a "Connection". Of course you should run these scripts as root.
Next I created below script to reconnect wifi after a suspend. This is after hours of hiting my head to the wall for why the wifi is not getting connected. The idea is to shutdown everything about wifi and then returning them back up. Even kernel module of wifi driver will be removed from the kernel (in my case brcmsmac) and then returned back! If you are experiencing the same problem of wifi not getting connected after suspend, then trust me. This is the necessary step network manager applets lack! So here you are the script:
Code:

echo "Shutting down previous wifi stuff..."
dhclient -r wlp2s0b1
pkill wpa_supplicant
ip link set wlp2s0b1 down
sleep 1
echo "Removing wifi driver module from kernel..."
modprobe -r brcmsmac
sleep 1
echo "Returning wifi driver module to kernel..."
modprobe brcmsmac
sleep 1
echo "Restarting wifi..."
ip link set wlp2s0b1 up
sleep 1
echo "Connecting to access point..."
wpa_supplicant -B -i wlp2s0b1 -c /etc/wpa_supplicant/$1
sleep 1
echo "Trying to get an IP..."
dhclient -v wlp2s0b1
ping -c 3 -w 3 www.google.com && echo "Successfully connected to wifi access point." && exit 0
echo "Couldn't make it work. Sorry I give up :-("
exit 1

So you just give it name of the wifi config file you created with previous script (Just filename. Not full path) and it turns everything off, sleeps for some seconds and then return them back up.
Things you should modify in above script are:
brcmsmac which is the driver kernel module of my wifi card. Yours might be different.
wlp2s0b1 which is the ugly name my linux gave to my wifi card (I don't get it what was the problem with easier names like wlan0?). Again yours might be different.
This should work 100 of time and you may now start a happy life. But remember:
If in future even this script faced problem, check if if these two processes are running frozen in background and kill them with -9 signal:
pkill -9 dhclient
pkill -9 wpa_supplicant
Sometimes you may also restart your access point. Access points are also computers after all.


All times are GMT -5. The time now is 10:23 PM.