Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Auto-disable AirPort when ethernet is active Network
Ever been annoyed to be on both wireless AND wired network at the same time, without the opportunity to automatically disable AirPort when plugging in a cable?

Well this little script and lauchd agent will auto-disable AirPort on active ethernet. It even notifies you using Growl, if installed.

Take this script and save it to /Library/Scripts/toggleAirport.sh. Go to that directory in Terminal and chmod the script (chmod 755 toggleAirport.sh will do) to be sure it's executable.
#!/bin/bash

function set_airport {

    new_status=$1

    if [ $new_status = "On" ]; then
	/usr/sbin/networksetup -setairportpower en1 on
	touch /var/tmp/prev_air_on
    else
	/usr/sbin/networksetup -setairportpower en1 off
	if [ -f "/var/tmp/prev_air_on" ]; then
	    rm /var/tmp/prev_air_on
	fi
    fi

}

function growl {

    # Checks whether Growl is installed
    if [ -f "/usr/local/bin/growlnotify" ]; then
	/usr/local/bin/growlnotify -m "$1" -a "AirPort Utility.app"
    fi

}

# Set default values
prev_eth_status="Off"
prev_air_status="Off"

eth_status="Off"

# Determine previous ethernet status
# If file prev_eth_on exists, ethernet was active last time we checked
if [ -f "/var/tmp/prev_eth_on" ]; then
    prev_eth_status="On"
fi

# Determine same for AirPort status
# File is prev_air_on
if [ -f "/var/tmp/prev_air_on" ]; then
    prev_air_status="On"
fi

# Check actual current ethernet status
if [ "`ifconfig en0 | grep \"status: active\"`" != "" ]; then
    eth_status="On"
fi

# And actual current AirPort status
air_status=`/usr/sbin/networksetup -getairportpower en1 | awk '{ print $4 }'`

# If any change has occured. Run external script (if it exists)
if [ "$prev_air_status" != "$air_status" ] || [ "$prev_eth_status" != "$eth_status" ]; then
    if [ -f "./statusChanged.sh" ]; then
	"./statusChanged.sh" "$eth_status" "$air_status" &
    fi
fi

# Determine whether ethernet status changed
if [ "$prev_eth_status" != "$eth_status" ]; then

    if [ "$eth_status" = "On" ]; then
	set_airport "Off"
	growl "Wired network detected. Turning AirPort off."
    else
	set_airport "On"
	growl "No wired network detected. Turning AirPort on."
    fi

# If ethernet did not change
else

    # Check whether AirPort status changed
    # If so it was done manually by user
    if [ "$prev_air_status" != "$air_status" ]; then
	set_airport $air_status

	if [ "$air_status" = "On" ]; then
	    growl "AirPort manually turned on."
	else
	    growl "AirPort manually turned off."
	fi

    fi

fi

# Update ethernet status
if [ "$eth_status" == "On" ]; then
    touch /var/tmp/prev_eth_on
else
    if [ -f "/var/tmp/prev_eth_on" ]; then
	rm /var/tmp/prev_eth_on
    fi
fi

exit 0
Then create a plist (below) and save it to

/System/Library/LaunchAgents/com.mine.toggleairport.plist
<?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">
<dict>
  <key>Label</key>
  <string>com.asb.toggleairport</string>
  <key>OnDemand</key>
  <true/>
  <key>ProgramArguments</key>
  <array>
    <string>/Library/Scripts/toggleAirport.sh</string>
  </array>
  <key>WatchPaths</key>
  <array>
    <string>/Library/Preferences/SystemConfiguration</string>
  </array>
</dict>
</plist>
Enjoy! Now when you plug in a cable, your AirPort will disable as soon as the ethernet connection becomes valid.

PS. It is also state aware. So if you turn your AirPort subsequently after plugging in a cable, it will not automatically turn it off again.

[crarko adds: I haven't tested this one. I put up the script and plist file here in a zipped folder for download.]
    •    
  • Currently 4.17 / 5
  You rated: 5 / 5 (12 votes cast)
 
[36,433 views]  

Auto-disable AirPort when ethernet is active | 39 comments | Create New Account
Click here to return to the 'Auto-disable AirPort when ethernet is active' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Auto-disable AirPort when ethernet is active
Authored by: blorg on Oct 06, '10 07:53:25AM

777 perms are dangerous (anyone can edit/modify the file). On a Mac, probably not all that important, as they tend to be single-user systems, but reasonable UNIX practices should still be used. I would recommend making the file owned by someone other than the primary user and making the file mode 755, that way it can't be modified by a drive by scripter and have things executed as the logged in user.



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: crarko on Oct 06, '10 08:38:48AM

That was my fault. Fixed per your suggestion.

Thanks,

Craig A.



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: RaceTripper on Oct 06, '10 07:56:28AM

Or you could make it easy. Go into the Network preference pane and change the service order to put Ethernet above Airport. Then it doesn't matter if Airport is on or not. Ethernet will take precedence. It's worked well for me for years.



[ Reply to This | # ]
Re-order
Authored by: mdwittenberg on Oct 06, '10 08:57:31AM

My thoughts exactly RaceTripper. Is you solution as good as this complicated solution?



[ Reply to This | # ]
Re-order
Authored by: DaijDjan on Oct 06, '10 09:13:13AM

I dont think so.. though I am not 100% sure... more like 80% :)

the script will turn airport on / off for real
=> sets the power

dont think the system preferences do that
=> they only mark which is the PRIMARY connection
==> all others are still active though



[ Reply to This | # ]
Re-order
Authored by: jonas_jonas on Oct 08, '10 08:13:14AM

And if you have installed blueutil then you can start and stop bluetooth with it.

I use this, because I only use the bluetooth mouse and keybord when using ethernet and my external monitor. So I can automatically deactivate bluetooth on the road to save a bit of battery.

Here are the changes in the first function:

function set_airport {
    new_status=$1

    if [ $new_status = "On" ]; then
	/usr/sbin/networksetup -setairportpower en1 on
        /usr/local/bin/blueutil off
	touch /var/tmp/prev_air_on
    else
	/usr/sbin/networksetup -setairportpower en1 off
        /usr/local/bin/blueutil on
	if [ -f "/var/tmp/prev_air_on" ]; then
	    rm /var/tmp/prev_air_on
	fi
    fi
}


[ Reply to This | # ]
Re-order
Authored by: ars on Oct 06, '10 09:54:13AM

I have rank ordered it like you suggest, but the hint is clearly superior. If e.g. your wireless access has a limited pool of IP numbers (which is the case at my work), then not having wireless active when connected by ethernet will not use up an IP number. I also have occasionally had problems with waking up my laptop, when it would grab the wireless access instead of the ethernet.



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: achitnis on Oct 06, '10 08:46:25AM

Do I see a missing "n" in /System/Library/LauchAgents/com.mine.toggleairport.plist :)



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: crarko on Oct 06, '10 09:34:05AM

Not any more. ;)



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: IkeaPimp on Oct 06, '10 09:50:07AM

Is there some service that needs to be restarted to get this working?



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: cleanhead on Oct 06, '10 09:58:13AM

Click on the AirPort status icon on the menu bar. Now click on "Turn AirPort Off." How hard is that?



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: slswift on Oct 06, '10 11:09:43AM

It's not hard. But it is easy to forget, hence the "auto" part of this hint.



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: dmmorse on Oct 06, '10 10:44:58AM

I'd like to modify this script to toggle my bluetooth when connecting to ethernet. I'm not a regular programmer, but I do dabble, so even though I'm not sure what setting to change within OS X to toggle bluetooth, i could probably do it with a little information/nudge in the right direction. Anybody with knowledge care to point me in the right direction? Your advice would be appreciated, plus I'll post my modifications back here.



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: pcurtner on Oct 06, '10 11:36:59AM

W/r/t to the previous comment about using the Network prefs, that misses one of the advantages of this script: conserving battery power.



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: ssevenup on Oct 06, '10 12:26:12PM

Those posting comments should realize that there are environments that require no other network active when connected to ethernet. Like government. So this solution can eliminate a need to switch network locations. Thanks for the tip.

---
Mark Moorcroft
ELORET Corp. - NASA/Ames RC
Sys. Admin.



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: allanmarcus on Oct 06, '10 12:41:07PM

very interesting, but it makes a few assumptions. 1, that ethernet is en0 and airport is en1. that is not always true. I think they are different on MacBook Airs.



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: blubdog on Oct 06, '10 01:10:05PM

I ran a diff on the shell script on the web page, and the script in the zip file. Line 57 is different. (the plist files are exactly the same)

if [ -f "./statusChanged.sh" ]; then
- "./statusChanged.sh" "$eth_status" "$air_status" &
+ "./statusChanged.sh" "$eth_status" "$air_status" &amp;
fi

The weird thing is it appears the zip file is wrong and the web page is correct.



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: crarko on Oct 06, '10 01:43:46PM

OK, I've made the correction to the uploaded file. Thanks.



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: rcsands on Oct 06, '10 01:55:51PM

OK, I'm not the quickest person but I can't get this to work. I copied the files as instructed, ran chmod 755 and nothing happens when I plugin the cable. I also get no notification when I manually enable/disable airport. I do have growlnotify installed. Can anyone help?



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: kopf on Oct 06, '10 05:05:44PM

Question:

Why should this go into /System/Library/LaunchAgents/ ?

I thought (and looking at the plists in that folder confirms it to me) that that folder should be reserved for Apple OEM system agents.

I'm thinking this should go into users daemons at /Library/LaunchDaemons/ which is where 3rd party software can put root level daemon plists such as sleepwatcher, Little Snitch, ncidd, etc. These will still be launched at system startup, without having to log in, although maybe later in the boot sequence.

Anything wrong with that thought process?

K



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: brettmjohnson on Oct 06, '10 05:23:47PM
It should be noted that this script works only on Snow Leopard (Mac OS 10.6) not Leopard (Mac OS 10.5).

The calling parameters to networksetup w.r.t. testing and setting airport power changed.
In 10.6, you specify the network device (en1), whereas in 10.5, you do not.

Also the generated output from /usr/sbin/networksetup -getairportpower changed, so the awk command needs to modified.
Basically, the changes to run on 10.5 are as follows:

In function set_airport, drop the device name from setairportpower:

 /usr/sbin/networksetup -setairportpower  on  (or off)
And also when getting the actual current Aiport status:
air_status=`/usr/sbin/networksetup -getairportpower | awk '{ print $3 }'`
Finally a test needs to change to be valid test syntax
# Update ethernet status
if [ "$eth_status" == "On" ]; then
should be fixed as:
# Update ethernet status
if [ "$eth_status" = "On" ]; then


[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: anthonysomerset on Oct 07, '10 03:18:47PM
note to actually set this working you need to load the plist into launchd, to do this
sudo launchctl load /System/Library/LaunchAgents/com.mine.toggleairport.plist
vice versa to disable this:
sudo launchctl unload /System/Library/LaunchAgents/com.mine.toggleairport.plist
also no growl notifications for me :(

[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: wildedave on Oct 08, '10 12:15:43AM

Growl Notifications not working for me either.

The script uses growlnotify which needs to be installed separately. The installer is in the Extras folder on the Growl disk image.
Install the package and the notifications will now work.

Well at least they did for me......



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: Anonymous on Oct 12, '10 12:47:11AM

Is anyone seeing this behavior? If, from bootup, Airport is off and ethernet is unplugged, upon login the script will turn Airport on, and then off, and then on, and then off, and so on. It also cycles Airport on and off if I unplug ethernet. not very reliable, what am I doing wrong? (original 17" MBP, SL 10.6.4)



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: agrabo on Oct 13, '10 10:45:01PM

Great post!

This worked like a charm.

Thanks again!



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: fatcat123 on Oct 21, '10 12:05:39PM
When using this script on Snow Leopard, if I had wireless disabled, shutdown, unplugged the network cable, and then booted back up, the script didn't auto enable the airport like it will on Leopard. I found that the plist was loaded as the user and not root. I moved the plist from /System/Library/LaunchAgents to /System/Library/LaunchDaemons. Upon reboot, the plist was loaded as root and resolved the issue not enabling the airport.

Also, to ensure to force the script to check the state at logout/reboot, I created a script and set a LogoutHook to clear the prev_air_on and prev_eth_on files at log out:

#!/bin/bash
cd /
airfile=/var/tmp/prev_air_on
ethfile=/var/tmp/prev_eth_on

if [[ -f $airfile ]]; then
	rm -f $airfile
fi
if [[ -f $ethfile ]]; then
	rm -f $ethfile
fi

touch /Library/Preferences/SystemConfiguration/toggleairport.txt
rm -f /Library/Preferences/SystemConfiguration/toggleairport.txt


[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: FaceTat on Nov 19, '10 03:59:05PM

Hi

I tried running the script and it works. I'm not a programmer or have any extensive knowledge of cmd lines, but, how do you disable this ? (I tried Google-ing how to undo chmod but got confused). Thanks ahead.



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: bert2d2 on Feb 03, '11 08:36:16AM

Hello,
Thanks for this great tip, as we have a lot of different machines, I am not sure we can apply the scripting solution.
I do however have this weird behaviour:
- when on airport-only i make a connection to a file-server
- if I then copy a large file: it is very slow
(so far normal behaviour)
- when I connect my ethernet-cable, I get an ip-address and also the service-order of this connection is higher than the airport connection
- If I then copy the large file again: it still is very slow
it's like the connection to the server is still on airport

When I disconnect and re-connect the server:
the file-copy goes fast (on ethernet-speed that is)

I suppose the server-session-connection (what's in a name) is still on airport, while the ethernet-connection is up-and-running.

Is this normal os-x behaviour?
Can I change it?
Can I reset the server-connection in some way (via a script?) without having to put user-name and password?

Any help would be gladly appreciated!
BerT



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: weking on Jun 28, '11 09:20:26AM

I installed this on two macs today and found an issue when waking from sleep when not connected to the ethernet. If you sleep with ethernet connected and airport off, then disconnect the ethernet and wake up, the airport does not automatically come on and if you manually turn it on it shuts off automatically. If you restart, you get to the blue apple screen and no farther. If you connect to the ethernet, boot works fine.



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: weking on Jun 28, '11 03:43:05PM

Turning airport on manually fails:

At least for OS 10.5:


# And actual current AirPort status
air_status=`/usr/sbin/networksetup -getairportpower en1 | awk '{ print $4 }'`

should read


# And actual current AirPort status
air_status=`/usr/sbin/networksetup -getairportpower en1 | awk '{ print $3 }'`



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: ratherbeintobago on Sep 18, '11 10:45:54AM

This is really good, but I sometimes get a Growl notification saying 'Airport Manually Turned Off' when waking from sleep with no ethernet connection; Airport is then turned off.

Anyone any ideas why this should be and how to fix it? OS10.7

Andy



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: Levitz on Sep 23, '11 03:03:59AM
Tried this, all works until loading it:

sudo launchctl load /System/Library/LaunchDaemons/com.mine.toggleairport.plist

launchctl: Dubious ownership on file (skipping): /System/Library/LaunchDaemons/com.mine.toggleairport.plist nothing found to load

Any ideas?

[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: TenOf11 on Oct 15, '12 11:35:49AM
I know this is an old post, but wanted to provide some additional information based on my experience implementing this method.

I see the following:
  • The plist file in the code provided is in LaunchDaemons. (not per instructions)
  • The owner of the plist file is something other than root. (not defined in instructions)
To correct the first, move the file:
sudo mv /System/Library/LaunchDaemons/com.mine.toggleairport.plist /System/Library/LaunchAgents/
To correct the second (after the first is done), change the owner on the plist file:
sudo chown root /System/Library/LaunchAgents/com.mine.toggleairport.plist
HTH

[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: tinkster on Oct 10, '12 08:03:41PM

Great script & method, works a treat; that is: works a treat if the machine has a built-in ethernet port. If it's a machine that uses a USB-Ethernet adapter the names of airport & wired are reversed. I don't know enough about Apple machines to find out whether there's a better method than the interface name (hard-coded in the script) to determine whether an interface is wired or not.


And I prefer this method over the "precedence" - far superior. No wasted power, no possibility of routing loops.



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: daniil on Oct 26, '13 01:50:53AM

I was using this script and enjoying it. However, when upgrading to Mavericks 10.9, the script caused the wifi connection to be dropped constantly. So, just a warning: don't use this with Mavericks.



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: HappyPig on Nov 02, '13 05:00:05PM

If only google had turned up Daniil's comment two weeks ago. I just spent those weeks with a Level 3 Apple Wifi specialist trying to troubleshoot why my Macbook Air was dropping it's connection every ten seconds.

It turns out it was this stupid script that was installed on a previous Macbook, forgotten about, then migrated over. And then the ticking script bomb went off in 10.9, and had me going nuts trying to figure out what happened.

So yeah, this script is deadly for Wifi under 10.9. If it helps the next victim track this down via Google, error -3903 kept showing up in the console during the disconnects.

Now I'm off to one-star the heck out of this hint.



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: tbussmann on Jul 16, '14 01:10:43AM

possibly, this is caused by launchdeamon respawning the script. I use a similar method (see http://www.computernetworkbasics.com/2012/12/automatically-turn-off-wireless-in-osx-including-mountain-lion/ ) where the author added a "sleep 10" at then end for this reason (see discussion)



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: lmadden on Apr 30, '14 08:45:25AM

Hi,

Tried this script on a 10.9.2 MBP with built in ethernet, and it works great. However, does not work on a MBP with retina display and T-bolt ethernet adaptor!! Airport constantly toggles on, then off, then on, then off, every 5-10 seconds or so whether ethernet is plugged in or not. Have tried leaving the adaptor plugged in, and makes no difference. changed the BSD name to en0 and then en4, but no change.

Any thoughts how to make this work on a newer MBP with either USB or T-bolt ethernet adaptor?

Thanks,

Lisa

---
Lisa Madden
Senior Systems Engineer - Field Tech Support
Apple Certified Desktop Technician
NASA Glenn Research Center



[ Reply to This | # ]
Auto-disable AirPort when ethernet is active
Authored by: devguydavid on Aug 30, '14 10:26:14AM
The assumption made is that en0 is the wired interface and en1 is the wireless. That's not always the case anymore. This is how to detect which is which.

Note that you need to change the Display Ethernet below to whatever title your own wired network adapter shows. Run networksetup -listnetworkserviceorder and look through the list to find what your value is. (Display Ethernet is what is shown for my Thunderbolt Display ethernet port.)

eth_dev=`networksetup -listnetworkserviceorder | sed -En 's/^\(Hardware Port: Display Ethernet, Device: (en.)\)$/\1/p'`
wifi_dev=`networksetup -listnetworkserviceorder | sed -En 's/^\(Hardware Port: (Wi-Fi|AirPort), Device: (en.)\)$/\2/p'`

Then, in the script, replace en0 with $eth_dev and en1 with $wifi_dev.

[ Reply to This | # ]