Skip to content

Instantly share code, notes, and snippets.

@jakewilliami
Forked from albertbori/Installation.md
Last active May 27, 2020 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakewilliami/b03e6eb61ce6a583860fd43a8befd3f9 to your computer and use it in GitHub Desktop.
Save jakewilliami/b03e6eb61ce6a583860fd43a8befd3f9 to your computer and use it in GitHub Desktop.
Automatically disable Wifi when an Ethernet connection (cable) is plugged in on a Mac

Overview

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.

Minimal changes to the script itself and addition of an install script by Jake Ireland, with suggestions from the community and with help from ShellCheck.

Requirements

  • Mac OSX 10+
  • Administrator privileges

Installation Instructions

Run

bash <(wget -q -O - "$(wget -q -O - 'https://gist.github.com/jakewilliami/b03e6eb61ce6a583860fd43a8befd3f9' | grep -E 'init.sh.*Raw' | awk -F'"' '{ print "https://gist.github.com/" $4 }')")

Uninstall Instructions

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

Misc

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

<?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.asb.toggleairport</string>
<key>OnDemand</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/Library/Scripts/toggleAirport.sh</string>
</array>
<key>WatchPaths</key>
<array>
<string>/Library/Preferences/SystemConfiguration</string>
</array>
</dict>
</plist>
#! /bin/bash
case $EUID in
*) : ;; # not root so we are okay
0) echo -e "\u001b[1;31mYou are running this script as sudo; exiting.\u001b[0;38m" && exit 1 ;; # we are running script as root so we need to exit (otherwise the script will not show notifications)
esac
TEMP_DIR="/tmp/eth-init/"
SCRIPTS_DIR="/Library/Scripts/"
PLIST_DIR="/Library/LaunchAgents/"
notify_user() {
echo -e "\u001b[1;34m===>\u001b[0;38m\t\u001b[1;38m${1}\u001b[0;38m"
}
mkdir -p "${TEMP_DIR}"
notify_user "Downloading script and property list files from GitHub."
cd "${TEMP_DIR}"
sudo wget 'https://gist.github.com/jakewilliami/b03e6eb61ce6a583860fd43a8befd3f9/raw/11d0be5033c30e8a0aeb4576a563a7b527857be3/toggleAirport.sh'
sudo wget 'https://gist.github.com/albertbori/1798d88a93175b9da00b/raw/21794addaf65ba3beae0600009c85ca072f6c335/com.mine.toggleairport.plist'
notify_user "Copying files to the appropriate directories in your root's Library."
sudo cp ./toggleAirport.sh "${SCRIPTS_DIR}"
sudo cp ./com.mine.toggleairport.plist "${PLIST_DIR}"
cd - > /dev/null
notify_user "Making these files executable for your user."
sudo chmod u+x "${SCRIPTS_DIR}"/toggleAirport.sh
sudo chown root "${PLIST_DIR}"/com.mine.toggleairport.plist
sudo chmod 644 "${PLIST_DIR}"/com.mine.toggleairport.plist
notify_user "Launching script daemon."
sudo launchctl load /Library/LaunchAgents/com.mine.toggleairport.plist
notify_user "Should be all up and running now :)"
#!/bin/bash
function set_airport {
new_status=$1
if [ "${new_status}" = "On" ]; then
/usr/sbin/networksetup -setairportpower "${air_name}" on
touch /var/tmp/prev_air_on
else
/usr/sbin/networksetup -setairportpower "${air_name}" off
if [ -f "/var/tmp/prev_air_on" ]; then
rm /var/tmp/prev_air_on
fi
fi
}
function growl {
# Checks whether Growl is installed
if command -v growlnotify; then
growlnotify -m "$1" -a "AirPort Utility.app"
else
osascript -e "display notification \"$1\" with title \"WiFi Toggle\""
fi
}
# Set default values
prev_eth_status="Off"
prev_air_status="Off"
eth_status="Off"
# 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: .*(Slot 1\|Ethernet\|LAN).*, Device: (en.+)\)$|\2|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
prev_eth_status="On"
fi
# Determine same for AirPort status
# File is prev_air_on
if [ -f "/var/tmp/prev_air_on" ]; then
prev_air_status="On"
fi
# Check actual current ethernet status
for eth_name in ${eth_names}; do
if [ "${eth_name}" != "" ] && [ "$(ifconfig "${eth_name}" | grep "status: active")" != "" ]; then
eth_status="On"
fi
done
# 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 "./statusChanged.sh" ]; then
"./statusChanged.sh" "${eth_status}" "${air_status}" &
fi
fi
# 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."
else
set_airport "On"
growl "No wired network detected. Turning AirPort on."
fi
# If ethernet did not change
else
# 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."
else
growl "AirPort manually turned off."
fi
fi
fi
# Update ethernet status
if [ "${eth_status}" == "On" ]; then
touch /var/tmp/prev_eth_on
else
if [ -f "/var/tmp/prev_eth_on" ]; then
rm /var/tmp/prev_eth_on
fi
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment