Saturday
We are trying to write a script to output all the active wifi and ethernet connections, if any, and turn off the wifi if there is an active ethernet connection. This is not working at all, because ifconfig is not returning the names of the connections (which may or may not be ethernet). networksetup -listallhardwareports is better as it displays the name of the "hardware port" but doesn't list the status. This seems promising but we haven't been able to use it in a script https://developer.apple.com/documentation/systemconfiguration/1517371-scnetworkinterfacegetinterface...
#!/bin/bash
# Define the names of the interfaces
WI_FI_INTERFACE="Wi-Fi"
ETHERNET_INTERFACE="Ethernet"
# Check if the Ethernet interface is active
ethernet_status=$(ifconfig "$ETHERNET_INTERFACE" | grep "status: active")
if [ -n "$ethernet_status" ]; then
echo "Ethernet is active. Turning off Wi-Fi..."
networksetup -setairportpower "$WI_FI_INTERFACE" off
else
echo "Ethernet is not active. Turning on Wi-Fi..."
networksetup -setairportpower "$WI_FI_INTERFACE" on
fi
Sunday - last edited Sunday
"networksetup -listnetworkserviceorder" will give you the actual interface name as well as the user-friendly name. This will return the interface name of the Hardware Port containing "Wi-FI" (typically en0):
wifi=$(networksetup -listallhardwareports | awk '/Hardware Port: Wi-Fi/{getline; print $NF}')
Then call ifconfig for the interface name:
ifconfig -u "${wifi}" | awk '/status/ { print $NF }'
..which will return active or inactive.
You might want to consider some other logic when changing networking settings, for example pinging an internal host or a publicly-available Internet host, or perhaps adding a few loops and exiting if nothing is reachable.
I also had this EA, might give you some ideas:
#!/bin/bash
interfaces=$(networksetup -listallhardwareports | awk '
/Hardware Port/ { port=substr($0, index($0, $3)) }
/Device/ { device=$2 }
/Ethernet Address/ {
if (device != "") {
cmd="ifconfig " device " | grep -q \"status: active\""
exit_code = system(cmd)
if (exit_code == 0) {
print port " (" device ")"
}
}
}')
echo "<result>"$interfaces"</result>"
Sunday
This is great, it gives me this:
<result>Thunderbolt Ethernet Slot 0, Port 1 (en6) Wi-Fi (en0)</result> when both are connected and
<result>Thunderbolt Ethernet Slot 0, Port 1 (en6)</result> when only ethernet is connected.
So now I just need a simple way to operationalize this and execute networksetup -setairportpower "$WI_FI_INTERFACE" off?
Sunday
There is not a functional way to mimic LAN WAN switching that you see on Windows laptops. MacOS prioritizes the ethernet network service over the WiFi service, but has no way to automatically disable the WiFi service if ethernet is present.
Anything Jamf does will be check in based. If the device has Ethernet, Jamf won't know it until the device checks in next which would be upwards of 20-30 minutes later. Once Jamf is aware of the Ethernet it can trigger whatever policy, which will run at next check in, so this entire workflow is 30-60min delayed from real time. You could look in to writing a launch agent to run a script on loop, but this will eat a LOT of CPU cycles.
Sunday
What about using the "on network change" event?
Monday - last edited Monday
Still not fast enough. The check-in will occur and complete within about 7-15 minutes after the network change, then add the check-in time skew. A policy will not feasibly run for upwards of 20-30 minutes after the network change occurs. Running something like this on network change will generate a lot of noise and possibly cause issues, heck this policy may trigger a "network change" event and loop itself as you are dumping a network adapter.
What problem are you trying to solve? There may be another way.
Monday
Good question. We are seeing the hostname be automatically changed when the hosts are connected to both. Yes we could use separate VLANs in theory but with some of the legacy equipment we have it's not really an option.