MBP Wi-Fi when docked

My MBP is docked most of the time, connected to a Belkin Thunderbolt 3 dock. The dock is connected to the network via ethernet.
So far I’ve switched off Wi-Fi when the MBP was docked. Mostly because I see that even though the ethernet connection has preference, the Wi-Fi connection still obtains an IP address. So it might even be used (which I don’t want/is not necessary).
I’m wondering what " best practice" is in this case: switch off Wi-Fi, or just leave it on? How can I see which network connection is really used?

1 Like

I leave mine on, largely because it’s necessary for certain services like unlocking with my watch and Handoff related functions.

The network connection that’s used is the one that is highest on your list of active network interfaces in the System Preferences app. I’ve verified this for myself enough times (usually when doing something else with tcpdump on a linux box) that I’m satisfied that it’s not sneakily changing when I’m not looking :slight_smile:

3 Likes

I don’t have a Mac to hand, but you used to be able to order your network interfaces in the Network sections of preferences so that your Mac knows the order in which to use them.

It’s worth checking whether your Belkin Network Interface shows up in the list and if it does, move it to the top.

1 Like

I think the most usual situation is to just make sure that the dock is hired in the Network Preferences in System Preferences.

As far as which one is used… you could use iStat Menus, I believe, to see which interface is being used.

You could disable Wi-Fi when the dock is plugged in (and re-enable it when disconnected). I’d recommend Keyboard Maestro for that.

1 Like

Thanks all. The reason for my question was that even though the ethernet connection was the preferred one (i.e. on top of the list) the Wi-Fi still got an ip address from the DHCP server. I found that peculiar and a bit confusing.

For now I have one profile (again) with both ethernet and Wi-Fi and ethernet at the top of the list.

@tjluoma Creating automatic switching of Network Locations using Hammerspoon has been on my to-do list for a long time. Although perfectly doable, it’s not straightforward. For one, Hammerspoon doesn’t have a Thunderbolt trigger, so I need to play around with other interfaces to detect the docking station (USB audio e.g.). Secondly, switching profiles is a non-trivial task in Hammerspoon (I know, I could use that other automation tool :wink: ).

That is how it works, but – now that you mention it – it is odd. I presume it is so that if the wired connection goes down, you already have a wireless connection, but I have no idea if that is true or not.

I am all in favor of people using their preferred tools.

However, I think it’s possible to do this for free, using all native Unix tools that Apple provides.

A Shell Script to Turn Airport/Wi-Fi On or Off When There Is a Network Change

The first piece is to have a shell script which checks to see if your wired connection has an IP address, and if it does, then turn off your Wi-Fi/AirPort connection.

I’m going to refer to this as /usr/local/bin/on-network-change.sh but obviously you can change /usr/local/bin/on-network-change.sh to whatever you want. Just make sure that it is executable:

chmod 755 /usr/local/bin/on-network-change.sh

(You may also need to create /usr/local/bin/ if it doesn’t exist, or you could make a folder like $HOME/bin/ instead, just change the paths.)


WARNING! ASSUMPTION AHEAD!

I’m also going to assume that your wired Ethernet port is en0 and your Wi-Fi/AirPort is en1 which may not be true and we ought to check using the output of ifconfig when you are wired in and compare it to the output of ifconfig when you are wireless.


This script will check to see if you have an IP address for en0.

If yes, it will turn off Wi-Fi.

If no, it will turn on Wi-Fi.

#!/bin/zsh -f

PATH="/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin"

WIRED_IP=$(ifconfig en0 | awk -F' ' '/inet /{print $2}')

if [[ "$WIRED_IP" == "" ]]
then
		# there is no IP address for the wired Ethernet port
	networksetup -setairportpower en1 on

else

		# there IS an IP address for the wired Ethernet port
	networksetup -setairportpower en1 off
fi

exit 0

So, that’s the script. Now the question is: how do we get it to run automatically as needed?

launchd to check for network changes

launchd is the chief scheduling tool on your Mac. You can tell it to run commands every X seconds, or when things happen.

Several years ago, I learned that the folder /Library/Preferences/SystemConfiguration/ will change wheenver there is “a change to the network settings”.

Obviously “a change” is a large “umbrella” but it includes things like getting an IP address for your wired connection, or disconnecting your wired connection.

However, we can use this to say “Any time there is a network change, tell me if I need to turn my AirPort/Wi-Fi on or off.”

Save the code block below to a file named something like $HOME/Library/LaunchAgents/com.tjluoma.on-network-change.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.tjluoma.on-network-change</string>
	<key>Program</key>
	<string>/usr/local/bin/on-network-change.sh</string>
	<key>RunAtLoad</key>
	<true/>
	<key>WatchPaths</key>
	<array>
		<string>/Library/Preferences/SystemConfiguration/</string>
	</array>
</dict>
</plist>

It must be a plain text file, it must be in that folder, and it must end with .plist.

(Note that /usr/local/bin/on-network-change.sh should be changed to reflect the name and path chosen in the previous step.)

Once the file is in place, you need to tell launchd to load it so that it will be used:

launchctl load "$HOME/Library/LaunchAgents/com.tjluoma.on-network-change.plist"

Note: To turn this off, do

launchctl unload "$HOME/Library/LaunchAgents/com.tjluoma.on-network-change.plist"

To stop it from launching when you reboot your Mac, move it to another folder or delete it.

First Step

The first thing we need to do is find out the actual interfaces to use instead of en0 and en1.

The easiest way I can think of to do this is to run ifconfig when you are not connected to your wired Ethernet connection, like this:

ifconfig > ~/Desktop/WirelessOnly.txt

and then run it again when you are connected to your wired connection (leave your Wi-Fi connection on for the time being).

Run it a second time when connected, like this:

ifconfig > ~/Desktop/WiredAndWireless.txt

You can either send those to me via PM or post them on the forum (there’s no security risk here, FYI).

3 Likes

I expect this tbh. The prioritization is purely that it doesn’t completely disable. So you’re still connected to both. I very specific case I use it for is running on 2 separate networks and splitting the traffic. This is pretty common in the entertainment industry either a lighting console requires the 2 networks or you want to send commands to lighting and another to sound, but they are on two different networks.

If you don’t want to use the Wi-Fi, you’ll have to disable it. I just keep mine on, so I don’t have to worry about it and for Apple Watch unlock and handoff functionality. Also I think Air Drop requires WiFi.

1 Like

Thanks @tjluoma. I’ll have a look at it. As someone who manages several linux servers for fun, I’m not afraid of the terminal. :wink:

1 Like