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.
- Mac OSX 10+
- Administrator privileges
- Copy
toggleAirport.sh
to/Library/Scripts/
- Run
chmod 755 /Library/Scripts/toggleAirport.sh
- Copy
com.mine.toggleairport.plist
to/Library/LaunchAgents/
- Run
chmod 600 /Library/LaunchAgents/com.mine.toggleairport.plist
- Run
sudo launchctl load /Library/LaunchAgents/com.mine.toggleairport.plist
to start the watcher
- 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
To debug, just run: sudo /Library/Scripts/toggleAirport.sh
and add echo's wherever you'd like
Thanks for the quick reaction.
It does not run on my end as It still bails out with 'Multiple ethernet interfaces match as I have e.g. in the office here multiple Display Ethernet and multiple LAN:
$ networksetup -listnetworkserviceorder | grep -e 'Display Ethernet' -e LAN
(1) Belkin USB-C LAN
(Hardware Port: Belkin USB-C LAN, Device: en8)
(3) Display Ethernet
(Hardware Port: Display Ethernet, Device: en9)
(6) Display Ethernet 2
(Hardware Port: Display Ethernet, Device: en10)
(7) Display Ethernet 3
(Hardware Port: Display Ethernet, Device: en11)
(11) USB 10/100/1G/2.5G LAN
(Hardware Port: USB 10/100/1G/2.5G LAN, Device: en6)
The script output is the following (I'm connecte to ethernet actually:
$ ./wifi-toggle.sh run
DEBUG: get_interface(): regex '(Wi-Fi|Airport)' -> interface 'en0'
DEBUG: Checking ethernet regexes: LAN Ethernet
DEBUG: Trying ethernet regex: 'LAN'
ERROR: Multiple ethernet interfaces match: LAN
DEBUG: No interface found for regex 'LAN'
DEBUG: Trying ethernet regex: 'Ethernet'
ERROR: Multiple ethernet interfaces match: Ethernet
DEBUG: No interface found for regex 'Ethernet'
DEBUG: No active ethernet interfaces found
DEBUG: ethernet status: 'inactive', wifi status: 'inactive'
DEBUG: enabling wifi
Actually I played around aswell with the script and came up with a solution that supports multiple matches with one REGEX and just checks if at least one interface is connected. The script is attached.
The only thing that actually isn't covered properly and should be covered the same way as in your original script is the case if the Wifi REGEX matches more than one interface.
I've also added a quick command 'list' to see what interfaces matches the different REGEX to double check if all needed is covered.
My version can be found at https://gist.github.com/keenonkites/9dc158bd63c43f6ff294ed9dd34bea31
Have a look and take whatever snippets you want (if any) for backporting.
Cheers Patrik