Skip to content

Instantly share code, notes, and snippets.

Last active June 15, 2024 02:19
Show Gist options
  • Save albertbori/1798d88a93175b9da00b to your computer and use it in GitHub Desktop.
Save albertbori/1798d88a93175b9da00b to your computer and use it in GitHub Desktop.
Automatically disable Wifi when an Ethernet connection (cable) is plugged in on a Mac
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">


This is a bash script that will automatically turn your wifi off if you connect your computer to an ethernet connection and turn wifi back on when you unplug your ethernet cable/adapter. If you decide to turn wifi on for whatever reason, it will remember that choice. This was improvised from this mac hint to work with Yosemite, and without hard-coding the adapter names. It's supposed to support growl, but I didn't check that part. I did, however, add OSX notification center support. Feel free to fork and fix any issues you encounter.

Most the credit for these changes go to Dave Holland.


  • Mac OSX 10+
  • Administrator privileges

Installation Instructions

  • Copy to /Library/Scripts/
  • Run chmod 755 /Library/Scripts/
  • Copy com.mine.toggleairport.plist to /Library/LaunchAgents/
  • Run chmod 600 /Library/LaunchAgents/com.mine.toggleairport.plist
  • Run sudo launchctl load /Library/LaunchAgents/com.mine.toggleairport.plist to start the watcher

Uninstall Instructions

  • Run sudo launchctl unload /Library/LaunchAgents/com.mine.toggleairport.plist to stop the watcher
  • Delete /Library/Scripts/
  • Delete /Library/LaunchAgents/com.mine.toggleairport.plist
  • Delete /private/var/tmp/prev_eth_on
  • Delete /private/var/tmp/prev_air_on


To debug, just run: sudo /Library/Scripts/ and add echo's wherever you'd like

function set_airport {
if [ $new_status = "On" ]; then
/usr/sbin/networksetup -setairportpower $air_name on
touch /var/tmp/prev_air_on
/usr/sbin/networksetup -setairportpower $air_name off
if [ -f "/var/tmp/prev_air_on" ]; then
rm /var/tmp/prev_air_on
function growl {
# Checks whether Growl is installed
if [ -f "/usr/local/bin/growlnotify" ]; then
/usr/local/bin/growlnotify -m "$1" -a "AirPort"
osascript -e "display notification \"$1\" with title \"Wifi Toggle\" sound name \"Hero\""
# Set default values
# Grab the names of the adapters. We assume here that any ethernet connection name ends in "Ethernet"
eth_names=`networksetup -listnetworkserviceorder | sed -En 's|^\(Hardware Port: .*Ethernet, Device: (en.)\)$|\1|p'`
air_name=`networksetup -listnetworkserviceorder | sed -En 's/^\(Hardware Port: (Wi-Fi|AirPort), Device: (en.)\)$/\2/p'`
# 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
# Determine same for AirPort status
# File is prev_air_on
if [ -f "/var/tmp/prev_air_on" ]; then
# Check actual current ethernet status
for eth_name in ${eth_names}; do
if ([ "$eth_name" != "" ] && [ "`ifconfig $eth_name | grep "status: active"`" != "" ]); then
# And actual current AirPort status
air_status=`/usr/sbin/networksetup -getairportpower $air_name | 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 "./" ]; then
"./" "$eth_status" "$air_status" &
# 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."
set_airport "On"
growl "No wired network detected. Turning AirPort on."
# If ethernet did not change
# 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."
growl "AirPort manually turned off."
# Update ethernet status
if [ "$eth_status" == "On" ]; then
touch /var/tmp/prev_eth_on
if [ -f "/var/tmp/prev_eth_on" ]; then
rm /var/tmp/prev_eth_on
exit 0
Copy link

kMikaZu commented Apr 16, 2024

@adamshand , what is the recommended path to put this script in? Certainly when using an MDM? What is your experience?

Copy link

@kMikaZu doesn't really matter where you put it. I put it in ~/bin/noarch on my laptop, if I was deploying via MDM I'd probably use /usr/local/bin.

Copy link

kMikaZu commented Apr 17, 2024

I used the original script and I get the following errors:

DEBUG: is_launchd_loaded(): nz.haume.wifi-toggle not loaded
Creating launchd service: /var/root/Library/LaunchAgents/nz.haume.wifi-toggle.plist
/usr/local/bin/ line 53: /var/root/Library/LaunchAgents/nz.haume.wifi-toggle.plist: No such file or directory
Enabling launchd service: nz.haume.wifi-toggle
Bootstrap failed: 125: Domain does not support specified action

Script is installed via Kandji with a Post Install script. The Bootstrap failed: 125 error has something to do with the fact that is has to be run as the current user. I tried some things but no luck so far.

Copy link

I don't know anything about Kandji sorry, I can help with that.

However it looks like you are installing it as root. I haven't tested that at all, it's designed to be installed by a normal user account.

Copy link

kMikaZu commented Apr 18, 2024

You have experience with adapting the code so it runs as the current user?

Copy link

The script is written and tested running as the normal user (not root). If you follow the instructions and install the script as normal user, it should (hopefully) work as intended. If you want it to run as root or to be installed by MDM you will need to test that and possibly make changes to the code.

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