How To Automatically Turn Off Airport When Ethernet is Plugged In Filed under: Technology — kendall @ 7:24 pm When I’m at work, I plug in my Ethernet cable and turn off my AirPort wireless network adapter. This is partly because the wifi in my office is a bit unreliable but also because the wired network is a lot faster. It’s not a major hassle to do this manually–it’s only two clicks to turn it off or on. Sure, over time that has added up to over a thousand clicks, still no biggie. But also, sometimes I forget to turn my wireless back on and those few seconds it takes to realize that my wireless is off and turn it on is a mild irritation. It also seems to cause Google Chrome to crash, often. So, cumulatively it adds up to a minor headache. So, I thought I’d automate this. I am indebted to this post: Auto-disable AirPort when ethernet is active. Most of what you need to make this work you can find in this post, though you might need to drill down into some of the comments. Here I’ve tried to document what I did to get it to work.
First you will need to copy this script into a text document and save it as /Library/Scripts/toggleAirport.sh.
#!/bin/bash
function set_airport {
new_status=$1
if [ $new_status = "On" ]; then
/usr/sbin/networksetup -setairportpower en0 on
touch /var/tmp/prev_air_on
else
/usr/sbin/networksetup -setairportpower en0 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
}
prev_eth_status="Off" prev_air_status="Off"
eth_status="Off"
if [ -f "/var/tmp/prev_eth_on" ]; then prev_eth_status="On" fi
if [ -f "/var/tmp/prev_air_on" ]; then prev_air_status="On" fi
if [ "ifconfig en1 | grep \"status: active\"
" != "" ]; then
eth_status="On"
fi
if [ "ifconfig en3 | grep \"status: active\"
" != "" ]; then
eth_status="On"
fi
if [ "ifconfig en6 | grep \"status: active\"
" != "" ]; then
eth_status="On"
fi
air_status=/usr/sbin/networksetup -getairportpower en0 | awk '{ print $4 }'
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
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
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
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 You will need to make the script executable. Open Terminal and change the permissions on the script by executing the following command:
chmod 755 /Library/Scripts/toggleAirport.sh Copy the following xml code into a text document and save as /System/Library/LaunchAgents/com.mine.toggleairport.plist
Label com.asb.toggleairport OnDemand ProgramArguments /Library/Scripts/toggleAirport.sh WatchPaths /Library/Preferences/SystemConfiguration You will need to load the plist into your launchctl daemon. You can do this by opening Terminal and executing the following command:sudo launchctl load /System/Library/LaunchAgents/com.mine.toggleairport.plist When I first tried this, I would get the error “Dubious ownership on file (skipping)”. I changed the ownership on the plist to match other launch agents with the following command and the plist loaded properly into launchctl:
sudo chown root:wheel /System/Library/LaunchAgents/com.mine.toggleairport.plist Now when I unplug my Ethernet cable my AirPort turns on and connects to known networks automatically, and when I plug in an Ethernet cable my AirPort turns off. Magic.