Auto-disable AirPort when ethernet is active

Oct 06, '10 07:30:00AM

Contributed by: Trenneren

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.]

Comments (39)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20100927161027611