Disable Airport when Ethernet is active

Contributor III

I would like to somehow automatically disable Airport when Ethernet is the active network connection. And potentially re-enable it when Ethernet is not in use.
I already have all my Macs configure with Ethernet at the top of the network service order.
In English the script would...
When a network change is detected, check to see if Ethernet is in use.
If Ethernet is in use turn the power off to Airport.
Else turn Airport on (or don’t do anything to Airport)
I know the command to turn the power off (/usr/sbin/networksetup -setairportpower off), but I am drawing a blank for how the detect the network change.

Do you think Casper can detect the network change, or is some LaunchD needed.

Eric Benfer
ITSD – Macintosh Services Manager
Johns Hopkins University Applied Physics Laboratory
11100 Johns Hopkins Rd
Laurel, MD 20723
eric.benfer at jhuapl.edu
443-778-4248 Balto.
240-228-4248 DC


Release Candidate Programs Tester

You could use the state change pkg in the Resource Kit to detect the change and have it run the command to change back and forth. Just curious though, if the order is right, why disable the airport?


John Wetter
Technical Services Manager
Educational Technology, Media & Information Services
Hopkins Public Schools

Not applicable

There was recently a MacOSXhints.com hint about this. I have implemented this on my machine and I love it. I personally had issues running iChat, or any other program that had an open connection when I plugged in the cable, as all the existing traffic would continue going through the airport, rather than the faster ethernet cable. I also noticed that Bonjour would act more reliable when only one connection was enabled.

Here's the hint (http://www.macosxhints.com/article.php?story100305114751547&query=disable+airport ). I had to modify the script a bit, so I'll put my modifications here as well.


if ifconfig en0 | grep inet;

then /usr/sbin/networksetup -setairportpower en1 off

/usr/sbin/networksetup -setairportpower en1 on
I changed it to turn the airport back on, and to turn the airport off if it detected the ethernet port had any IP address (could be a problem with a 169 address). I did not deploy this via Casper, but you could add the launchd item, and the script, but then it would not be controlled via Casper.


Honored Contributor

What is stopping the user from turning it back on?

New Contributor II

I believe that if you did it as a launchd item, that you could have launchd
trigger the script any time a specific file is touched, so if a user were to
turn the airport back on the script would immediately run and turn it back
off. If anyone knows the file that gets touched when changing a network
interface, I would be glad to hear which one. We have our wired and wireless
network assigning addresses from the same subnet and it would be nice to
keep the addresses handed out to a minimum as we don't want to create a
separate subnet for wireless.

Alan Benedict
Macintosh Technician
The Integer Group
O: 515-247-2738
C: 515-770-8234

Contributor II

Here is my script for doing this. I put it in my Every15 and am currently testing it. Seems to work fine so far. It does not toggle the AirPort back on and may not work for older OSX versions than say 10.7:

OS=`/usr/bin/sw_vers -productVersion | /usr/bin/colrm 5`
ACTIVEIF=`/usr/sbin/netstat -rn 2>&1 | /usr/bin/grep -m 1 'default' | /usr/bin/awk '{ print $6 }'`
WIFIIF=`/usr/sbin/networksetup -listallhardwareports | grep -A 1 Wi-Fi | awk '/Device/{ print $2 }'`
WIFIPWR=`/usr/sbin/networksetup -getairportpower $WIFIIF | sed 's/Wi-Fi Power.*: //g'`

if [ "$ACTIVEIF" == "$WIFIIF" ]; then
        exit 0
        if [ "$WIFIPWR" != "On" ]; then
                exit 0
                /usr/sbin/networksetup -setairportpower $WIFIIF off
                /usr/sbin/jamf displayMessage -message "Your wireless network adapter has been disabled as you are connected via Ethernet.  You will need to enable the Wi-Fi when disconnected from the Ethernet network."

Not applicable

For environments using various models with various 'en' labels, I've developed the following script to disable WiFi when an ethernet is detected and enable after ethernet is removed.


wifiDevice=`networksetup -listallhardwareports | grep 'Wi-Fi' -A1 | grep -o en.`
wifiStatus=`ifconfig "$wifiDevice" | grep 'status' | awk '{ print $2 }'`
ethernetDevice=`networksetup -listallhardwareports | grep 'en' | grep -v "$wifiDevice" | grep -o en.`
ethernetStatus=`for i1 in ${ethernetDevice};do
    echo $(ifconfig "$i1" | grep 'inet' | grep -v '|169.254.' | awk '{ print $2 }')
if [ "$ethernetStatus" ] && [ "$wifiStatus" = "active" ]; then
    $(networksetup -setairportpower "$wifiDevice" off)
    touch /var/tmp/wifiDisabled; fi
if [ "$ethernetStatus" = "" ] && [ "$wifiStatus" = "inactive" ]; then
    if [ -f "/var/tmp/wifiDisabled" ]; then
        rm -f /var/tmp/wifiDisabled
        $(networksetup -setairportpower "$wifiDevice" on); fi
exit 0

Use a launch agent or daemon to run at load and keep alive.

Valued Contributor II

Please check this out. Quite a few of us worked on this to perfect it. It's not mine it's ours :)


Not applicable

Modified to also audit AirPort power. Script now disables wireless interface even if not connected to an SSID.


# Define wireless interface "en" label.
wifiInterface=$(networksetup -listallhardwareports | grep 'Wi-Fi' -A1 | grep -o en.)
# Define wireless interface active status.
wifiStatus=$(ifconfig "${wifiInterface}" | grep 'status' | awk '{ print $2 }')
# Define wireless interface power status
wifiPower=$(networksetup -getairportpower "${wifiInterface}" | awk '{ print $4 }')
# Define non-wireless interface "en" labels.
ethernetInterface=$(networksetup -listallhardwareports | grep 'en' | grep -v "${wifiInterface}" | grep -o en.)
# Define non-wireless IP status.
ethernetIP=$(for i1 in ${ethernetInterface};do
  echo $(ifconfig "${i1}" | grep 'inet' | grep -v '127.0.|169.254.' | awk '{ print $2 }')
# Disable active wireless interface if non-wireless interface connected.
if [[ "${ethernetIP}" && "${wifiStatus}" = "active" ]] || [[ "${ethernetIP}" && "${wifiPower}" = "On" ]]; then
  networksetup -setairportpower "${wifiInterface}" off
  touch /var/tmp/wifiDisabled; fi
# Enable inactive wireless interface if previously disabled by daemon.
if [[ "${ethernetIP}" = "" && "${wifiStatus}" = "inactive" ]] || [[ "${ethernetIP}" = "" && "${wifiPower}" = "Off" ]]; then
  if [[ -f "/var/tmp/wifiDisabled" ]]; then
    rm -f /var/tmp/wifiDisabled
    networksetup -setairportpower "${wifiInterface}" on; fi
# Sleep to prevent launchd interpreting as a crashed process.
sleep 10
exit 0

Script is initiated by following Launch Daemon

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">

New Contributor II


thanks for the script it works well until one of our users plugs in their iPhone. Is there away of excluding iPhonesiPads so that if they plug in WiFi will stay active?



Contributor II

@designbridge I am having the same issue with the iPhone disconnects. Any idea how to fix that? Did you get it figured out?

Contributor II

I added the following

# Define iPhone interface "en" label.
iphoneInterface=$(networksetup -listallhardwareports | grep 'iPhone' -A1 | grep -o en.)

And then modified this line to add the iPhone interface

# Define non-wireless interface "en" labels.
ethernetInterface=$(networksetup -listallhardwareports | grep 'en' | grep -v "${wifiInterface},${iphoneInterface}" | grep -o en.)

That seemed to do the trick


fyi Under "Client-Side-Restrictions" there is the option to only run the Policy if Ethernet is available. I just have a simple Script to turn of WiFi and i'm running that with said restriction.


Valued Contributor

@claudiogardini ..... that's beautifully simple.

I can't believe I hadn't thought of that.

What about turning it back on?

Contributor II

@claudiogardini what a great idea! I second @Taylor.Armstrong how do you turn it back on?

New Contributor II

@ammonsc would it be possible to have another script to turn wifi on if there were no network connections?

New Contributor

Just a suggestion, we have used in the past an app named "lingon" https://itunes.apple.com/ca/app/lingon-3/id450201424?mt=12
We used an older version. Not sure about the current app. It allowed us to easily set up cron jobs, kill and start processes etc..