Automatically Turn off Wireless in OSX

The Problem:

On the Network that I’m in charge of there are hundreds of OSX desktop computers running a range of OS’s including Leopard, Snow Leopard, Lion, Mountain Lion, Mavericks, Yosemite, and so on.  Often I find the wireless card on these computers turned on even though they were connected to the wired network.  People just thought the wireless card had to be on for the computer to work.  But, turning the cards on often caused odd connectivity issues for people as well as kept using up a bunch of the wireless IP addresses by requesting a lease from our DHCP servers and then turning around an not using it.  Sometimes this would use up all the wireless IP addresses allocated at a site and would prevent people from being able to connect to the network!

The “I Did it For You” Solution:

If you just want this working and not have to open the terminal or implement the script below on your own I’ve got the thing for you. As a bonus to signing up for our email list I’ve built a pkg installer for this script. Just sign up for the list, download the pkg, and install it. We don’t SPAM, I promise. What we do, however, is provide more interesting content like this article to our email subscribers.

The “Do it Yourself” Solution:

To address this I wrote a script that turns off the wireless card on one of our computers if it is connected to our wired network.  If it is disconnected from our wired network, it automatically turns on the wireless card, providing a network connection failover for these computers.  As the article title suggests, I’ve updated the script to work on Leopard through Yosemite.

The Summary:

Basically, putting two files onto your computer is all that needs to be done.  Slap the two files onto your computer, tweak each one for your environment, and make one of them executable.  The first file is a script that, when run, will check to see if your computer is connected to the wired network, then turn on or off your wireless network card appropriately.  The second file is a “plist”, or configuration file, that is named a special way and put in a special location.  Once in place, any time any network interface changes its state (i.e. is turned on or off) this plist tells the computer to run the first file we put in place.  Here is a check list of the steps you will have to take:

  1. Configure the script file
  2. Put the script file into an appropriate location on your computer
  3. Make the script file executable
  4. Configure the “plist” file to point to your script file
  5. Put the “plist” file into the proper location on your computer
  6. Restart your computer

The Details:

Well, lets get to it.  Here are each of the steps in greater detail.  Note that you will have to open your terminal application to issue some of the commands that are found in these steps.

Configure the script file

I put the following script together.

Basically, this script pulls your systems OSX version and wired card IP info.  If the IP address is present then it issues the correct command for your OSX version to turn off your wireless card.  If the IP is not in the range you specified, or is nonexistent, then it turns on your wireless card.

To get the above script to work in your environment all you need to do is copy the above code into a text file on your computer, then make the file executable. We go over these steps below.

Put the script file into an appropriate location on your computer

I put the script file into a sub folder in a community scripts folder on my Mac’s.  This structure works well, because as you add more scripts and automate more tasks having a “normal” location that houses all your scripts is really useful.  The folder I’ve ended up using is /Library/Scripts/NetBasics/.  You can make sure this folder exists by issuing the following command: (note: sudo make you run as root and will prompt you for a password before it will run.  This is the password for your current account as long as it is allowed to administer the computer)

If you saved the script code above to a text file on your desktop named “wireless.sh” and you make the directory using the command just mentioned, then you can move the file into the folder by issuing the command:

Make the script file executable

Change file names as needed, but if you are using the filename wireless.sh in the “NetBasics” folder then you can make the script executable by issuing the following command:

Configure the “plist” file to point to your script file

Copy and paste the following code into an empty text file on your computer.  Name that file com.grivet-tools.wirelessonoff.plist.  Then edit the file if needed.  The “path” given in the configuration file below is consistent with the rest of this article.  If you changed the path to the script to something else then edit the path to that script file in the plist as well.

This plist basically links a trigger (the up/down state change for any of your network cards) to a task (run the other script that put in place).  So, any time your wired or wireless network card turns on/off or connects/disconnects the script that we put in place will run.

Put the “plist” file into the proper location on your computer

Make sure the folder that we are putting the file into exists:

Now put the plist into the following special location. The command assumes you worked on the file on your Desktop.

By putting the plist in this folder you are telling the OS to load the plist at boot and link the network card state change trigger to the script that we wrote.

Also, the plist needs to be owned by the user “root” and in the group “wheel” before it will work. (Thanks to Edward in the comments below for pointing that out!)  To change ownership of the plist issue the following command.

Restart your computer

Done!  Now if the script is configured correctly for your environment and everything is in the right place then if you plug your wired network card in your wireless card will automatically turn off.  If you try turning it back on it will just repeatedly turn itself back off.  Also, if you pull the wire out of the wired network card your wireless card will automatically turn itself back on.

This has been a very helpful script in the Mac environments that I work in!

NOTE: This article was migrated here from my Computer Network Basics blog because it makes more since here and so it would be a little more attention.




Interested In A Free Installer?

Our custom Wifi On/Off script has been a popular post!
This script automatically turns off the wireless interface on a computer when it is connected to a wired network, and turn the wireless interface back on when it is disconnected from the wired network.
This script has two main benefits:
  1. Network connection failover
  2. Eliminates unnecessary wireless traffic, and Multi-homed computers.
We are now offering a pkg installer for this script to anyone that signs up for our mailing list.