Turn wifi on/off based on ethernet status?

This should do it, and assumes some comfort with Terminal commands and the like.
You can use this script to set Wi-Fi based on the status of the Ethernet port (here assumed to be en0).

#!/bin/bash

ETHERNET_DEVICE=en0

NETWORKSETUP=/usr/sbin/networksetup
IFCONFIG=/sbin/ifconfig

WiFi=$($NETWORKSETUP -listallhardwareports | grep -A1 "Wi-Fi" | grep Device | cut -f2 -d' ')

echo "Run $0"
echo "Wifi interface $WiFi"
echo "Ethernet interface $ETHERNET_DEVICE"

EthernetStatus=$($IFCONFIG | grep -A5 $ETHERNET_DEVICE | grep status | cut -f2 -d' ')
echo "Ethernet is currently $EthernetStatus"

if [ "$EthernetStatus" == "inactive" ]; 
then 
	$NETWORKSETUP -setairportpower $WiFi on
	echo "Wi-Fi set on"
else
	$NETWORKSETUP -setairportpower $WiFi off
	echo "Wi-Fi set off"
fi

Now the problem is doing this automatically.
You could use KeyboardMaestro to run the script periodically.
Or you could use crontab.
Your crontab job (use crontab -e to edit it) would look like this to have the script run every minute:

*/1 * * * * ~/.local/bin/set_wifi_based_on_ethernet.sh

(I saved my set_wifi_based_on_ethernet.sh script into my ~/.local/bin folder. Be sure to make it executable.)

2 Likes