CentOS Wireless: Fix when it breaks!

Hi All – if you have ditched NetworkManager (as I have) and you are still running into wireless LAN access problems…check out this script!

I am in quite a rush so no time to explain the script…but check out the commands (and the order of the commands) and Enjoy 🙂

You must already have wpa_supplicant configured properly for these commands to work.


#!/bin/bash
# 20150217, ABr: Updated to have extensive additional code for wireless update

l_process=1
if ip a show wlan0 | grep --quiet -e "inet [0-9]\+\." 2>/dev/null; then
  echo "Looks like you already have a wireless IP. Is there a problem?"
  l_process=0
fi

l_fixed=1
if [ $l_process -eq 1 ]; then
  l_fixed=0

  # first, bring everything down
  sudo service network stop

  # next, unblock wifi
  sudo rfkill unblock wifi

  # bring up the network
  sudo service network start

  # do we have an IP?
  if ip a show wlan0 | grep --quiet -e "inet [0-9]\+\." 2>/dev/null; then
    echo "Wireless IP network found; we win."
    l_fixed=1
  else
    echo "No IP network found, bouncing wifi..."
    sudo ifdown wlan0
    # wait to give wifi time to initialize
    sleep 3
    sudo ifup wlan0

    # final check
    if ip a | grep --quiet -e "inet 192" 2>/dev/null; then
      echo "IP network found; victory!"
      l_fixed=1
    else
      echo "IP network not created, going for Round 2."
    fi
  fi
fi

if [ $l_fixed -eq 0 ]; then
  echo "So the wireless thinks it won't work, eh?"
  echo "Time for the heavy artillery."
  echo "Firing all salvos!"
  echo "  Stop the network (again)..."
  service network stop >/dev/null 2>&1
  echo "  Killing all wpa_supplicant instances..."
  l_wpas=$(ps -efa | grep wpa | grep -v grep | sed -e 's# \+# #g' | cut -d' ' -f 2)
  for i in $l_wpas; do
    kill -9 $i
  done
  echo "  Starting the network..."
  service network start >/dev/null 2>&1
  echo "  Force wireless down..."
  ifdown wlan0
  echo "  Unblock wireless..."
  rfkill unblock all
  echo "  Forcing the wireless card up..."
  ifconfig wlan0 up
  echo "  Forcing wpa_supplicant to start..."
  wpa_supplicant -iwlan0 -c/etc/wpa_supplicant/wpa_supplicant.conf  -Dwext &
  echo "  Forcing IP address..."
  ifup wlan0
  if ip a | grep --quiet -e "inet 192" 2>/dev/null; then
    echo "IP network found; victory!"
  else
    echo "No IP network, track down Andy again."
  fi
fi

Enjoy!

Team-oriented systems mentor with deep knowledge of numerous software methodologies, technologies, languages, and operating systems. Excited about turning emerging technology into working production-ready systems. Focused on moving software teams to a higher level of world-class application development. Specialties:Software analysis and development...Product management through the entire lifecycle...Discrete product integration specialist!

Leave a Reply

Your email address will not be published. Required fields are marked *

*