Skip to content

Instantly share code, notes, and snippets.

@alberthdev
Created January 31, 2017 14:41
Show Gist options
  • Save alberthdev/85aa09a6640ce64bb10655a3d9f75f4c to your computer and use it in GitHub Desktop.
Save alberthdev/85aa09a6640ce64bb10655a3d9f75f4c to your computer and use it in GitHub Desktop.
WiFi Reset Script (for Intel Wireless on Linux)
#!/bin/bash
# reset_wifi - WiFi Reset Script
# Copyright (C) 2014 Albert Huang.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# =====================================================================
# NOTES:
# This is designed for resetting iwlwifi, but it can be easily modified
# to reset other WiFi cards/drivers as well.
#
# Tested on Linux Mint 17 x64. This should probably work for many other
# Linux distros as well.
# ---------------------------------------------------------------------
# DEVELOPER NOTES:
# This was developed in an attempt to fix iwlwifi issues.
#
# I concluded that after everything was reset and done, you just have
# to kill wpa_supplicant one more time at the very end to make things
# work. Once that's done, the system may not reconnect for you, but if
# you reconnect yourself, this should work!
#
# I don't have enough time to test, but another possibility is that
# after waking up, simply killing wpa_supplicant should be enough to
# make things work (without going through the process of unloading
# and reloading iwlwifi). If someone could test this, I would
# appreciate it!
# Steps to test:
# (1) Use your laptop normally, suspend and resume when necessary.
# (2) When the WiFi fails to reconnect RIGHT AFTER RESUME, run:
# killall -9 wpa_supplicant
# (3) Try reconnecting manually.
# (4) If it works, yay! Report it to me and provide the following
# info:
# (a) Your Linux distro
# (b) Architecture (not that it matters, but...)
# (c) Type of Wifi network you tried to connect to.
# BE SPECIFIC. If WPA2, is it WPA2-Personal? Or
# WPA2-Enterprise? (Personal: simple Wifi password.
# Enterprise: username and password for Wifi.)
# Other options include WEP, etc. (specify length of
# password... but don't give me the password!)
# Thanks!
#
# chkmod: check if kernel module is loaded or not!
# Usage: chkmod mod_name
# Returns: 0 if found, 1 if not
# Example: chkmod iwlwifi
chkmod() {
mod_list=`lsmod | cut -f1 -d' '`
found=`printf "$mod_list" | grep "$1"`
if [ "$found" = "" ];then
return 1
fi
return 0
}
# chkproc: check if process is running or not!
# Usage: chkproc proc_name
# Returns: 0 if running, 1 if not
# Example: chkproc wpa_supplicant
chkproc() {
proc_list=`ps -A | tr -s ' ' | cut -f4 -d' '`
found=`printf "$proc_list" | grep "$1"`
if [ "$found" = "" ];then
return 1
fi
return 0
}
# showexec: display command to be executed!
# Usage: showexec command...
# Returns: nothing. Just prints " >> EXEC: command..."
# Example: showexec modprobe iwlwifi
# Prints:
# >> EXEC: modprobe iwlwifi
showexec() {
echo " >> EXEC: $@"
}
# Check to make sure we have root!
if [ ! "`whoami`" = "root" ];then
echo " * You need root to proceed. Launching as root via sudo..."
showexec sudo $0 $@
sudo $0 $@
exit $?
else
echo " * You're root! Proceeding to reset wifi..."
fi
# Remove necessary modules...
MODS="iwldvm iwlwifi mac80211 cfg80211"
for MOD in $MODS; do
chkmod $MOD
if [ ! "$?" = "0" ];then
echo " * Skipping kernel module $MOD, since it's not loaded."
continue
fi
echo " * Removing kernel module $MOD..."
showexec rmmod $MOD
rmmod $MOD
if [ ! "$?" = "0" ];then
echo "Failed to remove kernel module $MOD. See above for details."
exit 1
fi
done
# Pause to let things settle down!
echo " * Waiting 3 seconds for interface to shut off..."
showexec sleep 3s
sleep 3s
# Kill wpa_supplicant daemon...
echo " * Killing wpa_supplicant..."
showexec killall -9 wpa_supplicant
killall -9 wpa_supplicant
chkproc wpa_supplicant
if [ "$?" = "0" ];then
echo " * ERROR: Unable to kill wpa_supplicant."
exit 1
fi
# Reload necessary modules!
REL_MODS="iwlwifi"
echo " * Press ENTER now (within 5 seconds) to enter custom"
echo " module loading options."
read -t 5
if [ "$?" = "0" ];then
while :; do
read -p "Enter a module and its options (empty to finish): " mod_opts
if [[ -z "$mod_opts" ]]; then
echo " * Exiting custom module loading."
break
fi
showexec modprobe $mod_opts
modprobe $mod_opts
if [ ! "$?" = "0" ];then
echo "Failed to custom load kernel module [$mod_opts]. See above for details."
echo "To exit, press CTRL-C."
fi
done
else
for MOD in $REL_MODS; do
echo " * Reloading module: $MOD"
showexec modprobe $MOD
modprobe $MOD
if [ ! "$?" = "0" ];then
echo "Failed to load kernel module $MOD. See above for details."
exit 1
fi
done
fi
# Pause to let things settle down!
echo " * Waiting 3 seconds for interface to start working..."
showexec sleep 3s
sleep 3s
# Bring down and bring back up the interface!
INTERFACE="wlan0"
echo " * Bringing network interface $INTERFACE down..."
showexec ifconfig $INTERFACE down
ifconfig $INTERFACE down
echo " * Bringing network interface $INTERFACE up..."
showexec ifconfig $INTERFACE up
ifconfig $INTERFACE up
# Pause to let things settle down!
echo " * Waiting 15 seconds for interface to go up..."
showexec sleep 15s
sleep 15s
# Kill wpa_supplicant daemon...
# NOTE: This final kill seems to be the key to fixing the issue on iwlwifi
echo " * Killing wpa_supplicant..."
showexec killall -9 wpa_supplicant
killall -9 wpa_supplicant
chkproc wpa_supplicant
if [ "$?" = "0" ];then
echo " * ERROR: Unable to kill wpa_supplicant."
exit 1
fi
# Yay! Done!
echo ' * All done!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment