Automatically Disabling WiFi in macOS Mojave When Connecting to Ethernet

Standard

I don’t know why, but macOS doesn’t seem to have an automatic ability to shut down the WiFi adapter when an Ethernet connection is detected. It’s a simple thing that can cause several problems, especially if you are in an environment where you may need to authenticate to a network or access VPN services.

It may be possible to manage locations and some additional tools, but I didn’t want something that required retooling every time I used my system in a new location, so I decided to write a simple script, using built-in tools for everything except the execution of the script.

This script will check for both an active WiFi connection and also an active Ethernet connection. If it detects both, it will shutdown the WiFi interface and display a notification, via macOS notifications, that it has done this. I haven’t tested this on previous versions of macOS (only Mojave), though if the rest of the commands work, the script might function by simply removing the statement that generates a notification.

If you’re familiar with the underpinnings of macOS, you can certainly setup the script to run under one of the built-in scheduler tools. However, I’ve been a user of GeekTool for many years, so instead of using something different for only one custom script, I decided to use it to execute the tool. Note that adding the script as a launch item won’t solve your problem because that would only execute it once, when you login. You need to have it execute on a frequent interval in order for this to be useful. In my GeekTool settings it’s configured to run every 10 seconds.

You will need to know the device name (dev entry) for your WiFi interface and also your Ethernet interface. There are multiple ways to get this. Here’s one Terminal command that will accomplish this:

networksetup -listnetworkserviceorder

WiFi seems to usually be en0 in macOS, but that may not always be the case, and it’s very common for the Ethernet device to be something different from one system to the next.

Once you have those values, write them down. You’ll need them later.

Create a new file using any text editor (but make sure it will save in plain-text, without any formatting). Place it in a directory that you’re comfortable with executing a script like this and give it a name with a .sh extension. For example: wifidetect.sh

Place this code into the file and replace my values for the interfaces with the interface names that correspond to those on your system. The myENI value should correspond to your Ethernet interface device and myWIFI should match your WiFi interface device. Everything from “oasascript” to “Network Status”‘ should end up on a single line, though it may soft-wrap in your text editor.

!/bin/bash
 myENI=en8
 myWIFI=en0
 if [[ $(networksetup -getairportpower $myWIFI | grep 'On') ]]; then
     if [[ $(ifconfig $myENI | grep 'status: active') ]] ; then
     osascript -e 'display notification "Ethernet interface is active. Shutting down Wifi interface." with title "Network Status"'
     networksetup -setairportpower $myWIFI off
     fi
 fi

You’re almost done. The script needs to be executable, so open up Terminal and navigate to the directory where the script resides and then enter this command (if you used a different file name, simply replace wifidetect.sh with your file name):

chmod +x wifidetect.sh

The next step is to have it execute on a frequent interval. I’m not going to get into how to do that, other than to mention, again, that I’m using GeekTool to accomplish this.

I purposely did not have this script re-enable the WiFi interface whenever the Ethernet connection is deactivated. While that is a feature that many might prefer, in my case there may be times when I’d prefer to NOT have my system automatically connect to WiFi the instant that I disconnect from LAN. The purpose of this script is to avoid having multiple interfaces active, which can very easily go unnoticed. Yet, not having any active interfaces is immediately noticeable.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.