Yosemite Wi-Fi problems – sample “recover from network meltdown” script

Here’s the sample “bodge” script from our article about OS X Yosemite Wi-Fi problems.

You can copy-and-paste it from here:

#! /usr/bin/env bash 

while true; do
  echo "$(date -u): \c"  
  dig @8.8.8.8 +tries=1 +time=3 google.com | \
          grep 'Query time' | \
          cut -d ' ' -f 4 -f 5
  if [ ${PIPESTATUS[0]} -ne 0 ] ; then
     ifconfig en0 down
     ifconfig en0 up
     echo "--> Bounced en0\n"
  fi
  sleep 5
done

You’ll need to run the script via sudo to give it the root access it needs to stop and restart the Wi-Fi network interface.

You may wish to tweak some of the parameters.

Simply explained:

  • The dig +tries=1 +time=3 command does the lookup, waiting 3 seconds for an answer.
  • The grep and cut commands extract some handy feedback (the time taken if the DNS request succeeds).
  • ${PIPESTATUS[0]} is the return code of the first command (dig) in the pipeline above, which will be non-zero if there was an error.
  • The ifconfig down/up commands force the Wi-Fi network to reconnect if a DNS error occurred.
  • The device name en0 is the Wi-Fi network card on my Mac. Yours may be different.
  • The sleep 5 takes a 5-second break before trying again.

When the Wi-Fi network re-connects, traffic flows again until the next meltdown.

If you have any comments or suggestions, please leave them as comments against the parent article.

Thanks, and we hope you solve (or at least avoid!) any network problems you may be having.

One comment on “Yosemite Wi-Fi problems – sample “recover from network meltdown” script

  1. sudo ./wifi.sh
    Fri 25 Mar 2016 16:00:23 UTC: \c
    129 msec
    Fri 25 Mar 2016 16:00:28 UTC: \c
    41 msec
    Fri 25 Mar 2016 16:00:33 UTC: \c
    245 msec
    Fri 25 Mar 2016 16:00:39 UTC: \c
    435 msec
    Fri 25 Mar 2016 16:00:44 UTC: \c
    329 msec
    (Wifi turned off)….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s