Skip to content

Instantly share code, notes, and snippets.

@michaelbrawn
Created April 15, 2015 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelbrawn/d3b7d9cadc9b20024505 to your computer and use it in GitHub Desktop.
Save michaelbrawn/d3b7d9cadc9b20024505 to your computer and use it in GitHub Desktop.
Toggle Wifi On/Off when Ethernet changes

Taken from

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

}

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 en1 | grep \"status: active\"" != "" ]; then eth_status="On" fi

Check actual current ethernet status for Display Ethernet

if [ "ifconfig en3 | grep \"status: active\"" != "" ]; then eth_status="On" fi

Check actual current ethernet status for Thunderbolt Ethernet adapter

if [ "ifconfig en6 | grep \"status: active\"" != "" ]; then eth_status="On" fi

And actual current AirPort status

air_status=/usr/sbin/networksetup -getairportpower en0 | 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 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment