Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatically disable Wifi when an Ethernet connection (cable) is plugged in on a Mac
<?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>

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.

Requirements

  • Mac OSX 10+
  • Administrator privileges

Installation Instructions

  • 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

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

#!/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 [ -f "/usr/local/bin/growlnotify" ]; then
/usr/local/bin/growlnotify -m "$1" -a "AirPort Utility.app"
else
osascript -e "display notification \"$1\" with title \"Wifi Toggle\" sound name \"Hero\""
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: .*Ethernet, Device: (en.)\)$|\1|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
@vijaykiran

This comment has been minimized.

Copy link

@vijaykiran vijaykiran commented Nov 26, 2015

FYI - OnDemand is deprecated, you can use KeepAlive instead

@Redbeard25

This comment has been minimized.

Copy link

@Redbeard25 Redbeard25 commented Jan 12, 2016

I had to chown root my com.mine.toggleairport.plist file in order for it to run.

@Redbeard25

This comment has been minimized.

Copy link

@Redbeard25 Redbeard25 commented Jan 26, 2016

After a while, I started growling all over the place... telling me it needed to turn on the Airport with the Airport was already on. I'm guessing something messed up with var/tmp/prev_eth_on or var/tmp/prev_air_on?

@ChaosCoder

This comment has been minimized.

Copy link

@ChaosCoder ChaosCoder commented Jan 26, 2016

Yeah, had the same issue just some minutes ago. Both, Airport and Ethernet was connected, when unconnecting and reconnecting Ethernet it said: "No wired network detected. Turning AirPort on.". Deleting both /var/tmp/prev_* fixed it.

@Nicky-M

This comment has been minimized.

Copy link

@Nicky-M Nicky-M commented Feb 25, 2016

Hi everyone,

I am desperately looking for such option. I have tried your script on a MBAir with OS X 10.11.3 and it is working and switching between wireless and wired network but there is annoying notifications and when i click it, it opens the scripting tool. How can I get the notification in the "Notifications" but without been clickable. By the way, when I unplug the RJ45 adapter, it switch back to the Wireless but I have 6 or 7 notifications, how to avoid that.

Thanks a lot for your precious help.

PS: I know this post is quite old but I take my chance.

@EricKelley

This comment has been minimized.

Copy link

@EricKelley EricKelley commented Apr 27, 2016

What would be a good program or tool to use to create an installer for this script? I would like to deploy this to a couple hundred machines, and obviously do not want to have to do it manually. I would rather push an installer through JAMF or ARD.
thanks in advance.

EK

PS: I have already tested it on a couple machines running 10.11.4 and have yet to run into any issues.

@jreinstedler

This comment has been minimized.

Copy link

@jreinstedler jreinstedler commented Jun 6, 2016

LaunchAgents should have 644 permissions, not 600. :)

@tarpanpathak

This comment has been minimized.

Copy link

@tarpanpathak tarpanpathak commented Aug 16, 2016

Hello,
Just want to say thank you for this script. It's been deployed and works well.

@TheVendiniPhil

This comment has been minimized.

Copy link

@TheVendiniPhil TheVendiniPhil commented Nov 8, 2016

you forgot about Ethernet-via-dongle (which is becoming increasingly more common)

eth_names=networksetup -listnetworkserviceorder | sed -En 's|^\(Hardware Port: .*Ethernet.*, Device: (en.)\)$|\1|p'

@EliyahuStern

This comment has been minimized.

Copy link

@EliyahuStern EliyahuStern commented Jan 12, 2017

I kept getting
/Library/LaunchAgents/com.mine.toggleairport.plist: Path had bad ownership/permissions
To fix it I:

  1. Copied the contents of the plist and created a new one (the one I downloaded had extended attributes).
  2. chown root /Library/LaunchAgents/com.mine.toggleairport.plist
  3. sudo chmod 755 /Library/LaunchAgents/com.mine.toggleairport.plist
@Thorolf15

This comment has been minimized.

Copy link

@Thorolf15 Thorolf15 commented Feb 23, 2017

Hello,

Thanks for the script! Doesn't work for me though, but then again i have to use an adapter USB-Ethernet, maybe that is why.. Any idea how to change, if possible?

Have a good day.

@hunter0008

This comment has been minimized.

Copy link

@hunter0008 hunter0008 commented Mar 10, 2017

I was finally able to get past the bad ownership/permissions stage, but it still doesn't seem to be working for me. I'm hooked up with a dongle via thunderbolt 3.

@ldellus

This comment has been minimized.

Copy link

@ldellus ldellus commented Jun 7, 2017

Hello,

I just installed the script on MacOS Sierra 10.12.5 and it works fine with the following modifications:

I'm using a dongle for my wired connection so in the script toggleAirport.sh you have to change the following line:

eth_names=networksetup -listnetworkserviceorder | sed -En 's|^\(Hardware Port: .*Ethernet, Device: (en.)\)$|\1|p'
to
eth_names=networksetup -listnetworkserviceorder | sed -En 's|^\(Hardware Port: .*LAN, Device: (en.)\)$|\1|p'

and apply the changes described above:

chown root /Library/LaunchAgents/com.mine.toggleairport.plist

sudo chmod 755 /Library/LaunchAgents/com.mine.toggleairport.plist

Enjoy!

@paulbhart

This comment has been minimized.

Copy link

@paulbhart paulbhart commented Jun 13, 2017

went ahead and checked this into a github repo to enable others to save a few keystrokes.

https://github.com/paulbhart/toggleairport

@mnemo70

This comment has been minimized.

Copy link

@mnemo70 mnemo70 commented Jul 16, 2017

Thanks for the script!

I also have a USB ethernet adapter, which displays as:

(3) USB 10/100/1000 LAN
(Hardware Port: USB 10/100/1000 LAN, Device: en7)

I changed the eth_names line to this, which works for me, and it should still recognize "Ethernet" adapters as well. Note the "backslash 2" at the end of the line.

eth_names=`networksetup -listnetworkserviceorder | sed -En 's|^\(Hardware Port: .*(Ethernet\|LAN), Device: (en.)\)$|\2|p'`
@paul-cossey

This comment has been minimized.

Copy link

@paul-cossey paul-cossey commented Jul 26, 2017

Hi,

Trying to get this to work with a StarTech Thunderbot 3 dock (a seperate USB adapter works with the changes @mnemo70 detailed above)

The service is called Thunderbolt Ethernet Slot 1 and it's picking up en5

Anyone have any ideas?

Thanks
Paul

@paul-cossey

This comment has been minimized.

Copy link

@paul-cossey paul-cossey commented Jul 27, 2017

Managed to figure it out by adding in Slot 1\

eth_names=networksetup -listnetworkserviceorder | sed -En 's|^\(Hardware Port: .*(Slot 1\|Ethernet\|LAN), Device: (en.)\)$|\2|p'

@J-Camping

This comment has been minimized.

Copy link

@J-Camping J-Camping commented Aug 23, 2017

Just as a heads up if you have more than 9 ethernet connections on your system, you will need to modify the script to use:

eth_names=networksetup -listnetworkserviceorder | sed -En 's|^\(Hardware Port: .*(Slot 1\|Ethernet\|LAN), Device: (en.*.)\)$|\2|p'

This changes Device:(en.) to Device:(en.*.) which will allow for 2 decimal places in the adapter name. There may be a better way to do this in sed, but it fixed the issue I was having.

@maburdett

This comment has been minimized.

Copy link

@maburdett maburdett commented Aug 28, 2017

Every once and while, I get complaints of repeated notifications to turn on the Airport with the Airport was already on. Deleting both /var/tmp/prev_* will fix this issue.
I think what is happening is the user will dock the mac, they get a wifi authentication prompt, the scrip runs, but the wifi authentication prompt prevents wifi from getting shut off, they finish the authentication and now the /var/tmp/prev_air_on is out of sync.

Ideas how modify the script to check if the wi-fi was actually turned off before running the command rm /var/tmp/prev_air_on?

@digitalsr2

This comment has been minimized.

Copy link

@digitalsr2 digitalsr2 commented Dec 28, 2017

Updated: I got it to run but nothing is happening. I'm on High Sierra.

I can't seem to get past the /Library/LaunchAgents/com.mine.toggleairport.plist: Path had bad ownership/permissions

I've tried the various suggestions by people in comments. Also, does anyone know if this works on High Sierra?

@wagnergerald

This comment has been minimized.

Copy link

@wagnergerald wagnergerald commented Mar 31, 2018

Just tried it on high sierra 10.13.4 and it works.

As suggested above you need to change the ownership of the agent file.

@NoMoreWafflesz

This comment has been minimized.

Copy link

@NoMoreWafflesz NoMoreWafflesz commented Apr 5, 2018

Happen a copy of that changed ownership file? Never messed with XCode before.

@XfitWarri0r

This comment has been minimized.

Copy link

@XfitWarri0r XfitWarri0r commented Jun 19, 2018

Hi Team!,
I'm getting the following...

/Library/LaunchAgents/com.mine.toggleairport.plist: Invalid property list

Any idea as to why the "invalid property list"? Researching on my end as well. Will share findings if they are promising.

@RetepV

This comment has been minimized.

Copy link

@RetepV RetepV commented Aug 22, 2018

I would make the eth_name line as follows:

eth_names=`networksetup -listnetworkserviceorder | sed -En 's|^\(Hardware Port: .*(Slot 1\|Ethernet\|LAN), Device: (en[0123456789]+)\)$|\2|p'`

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Sep 7, 2018

at line 36 please change it to:

eth_names=`networksetup -listnetworkserviceorder | sed -En 's|^\(Hardware Port: .*Ethernet, Device: (en.+)\)$|\1|p'`

it will help like me if someone have Ethernet on en10 or higher

@synman

This comment has been minimized.

Copy link

@synman synman commented Sep 26, 2018

This thread rocks. thanks for the cool solution

@bossnass

This comment has been minimized.

Copy link

@bossnass bossnass commented Oct 23, 2018

you forgot about Ethernet-via-dongle (which is becoming increasingly more common)

eth_names=networksetup -listnetworkserviceorder | sed -En 's|^\(Hardware Port: .*Ethernet.*, Device: (en.)\)$|\1|p'

That is a valid one and I would like to share my experience here, since have lost some time to find out the following - once you have an iPhone connected over USB, you might find your Ethernet-via-dongle (in my case a docking station) port name changed the following way:

  1. Docking station port name with NO iPhone connected = (Hardware Port: Thunderbolt Ethernet Slot 1, Device: en9)
  2. Docking station port name with iPhone connected = (Hardware Port: Thunderbolt Ethernet Slot 1, Port 2, Device: en9)

Because of this, one might want to use the following regex (adding a third DOTALL, located right after "Ethernet.*,"):
eth_names=`networksetup -listnetworkserviceorder | sed -En 's|^(Hardware Port: .*Ethernet.*,.* Device: (en.))$|\1|p'

@Catbodi

This comment has been minimized.

Copy link

@Catbodi Catbodi commented Feb 19, 2019

Happen a copy of that changed ownership file? Never messed with XCode before.

this honestly one of the funniest things i've read on this site.

@Pistacchione

This comment has been minimized.

Copy link

@Pistacchione Pistacchione commented Mar 26, 2019

Hi I'm using an external hub with lan port, and changed the eth_nameslike this:

eth_names=networksetup -listnetworkserviceorder | sed -En 's/^\(Hardware Port: (.*Ethernet|.*LAN), Device: (en.)\)$/\2/p'

and changed the check if from

`if ([ "$eth_name" != "" ] && [ "`ifconfig $eth_name | grep "status: active"`" != "" ]); then`

to

`if ([ "$eth_name" != "" ] && [ "`ifconfig $eth_name | grep "status: active"`" != "" ] && [ "`ifconfig $eth_name | grep "status: active"`" != "inactive" ]); then`
@AshUK

This comment has been minimized.

Copy link

@AshUK AshUK commented Apr 1, 2019

couldn't be bothered learning sed script so used grep
eth_names=`networksetup -listnetworkserviceorder | grep -E 'Hardware Port.*Apple USB.*en' | awk -F "(, )|(: )|[)]" '{print $4}'`

@jeff-zapotoczny-sonarsource

This comment has been minimized.

Copy link

@jeff-zapotoczny-sonarsource jeff-zapotoczny-sonarsource commented Apr 24, 2019

There's no need to install this as sudo. In fact, registering it as your own user seems to be key to having the notifications actually display (at least on macOS Mojave).

@sungod23

This comment has been minimized.

Copy link

@sungod23 sungod23 commented Aug 6, 2019

using a new macbook pro with a thunderbolt dock, this was not picking up anything as the adapter was a thunderbolt bridge device.

eth_names=networksetup -listnetworkserviceorder | sed -En 's|^\(Hardware Port: .*(Slot 1\|Ethernet\|LAN\|Thunderbolt.*), Device: (en.*.\|bridge.*.)\)$|\2|p'

picked it up and worked perfectly.

@maronilover

This comment has been minimized.

Copy link

@maronilover maronilover commented Aug 25, 2019

Thank you so much for this script and thread! Worked for me with MB2017 and new HP Thunderbolt Dock G2, 120W.
Thanks for your additions sungod23.

Perfect!

@anthonyattard

This comment has been minimized.

Copy link

@anthonyattard anthonyattard commented Sep 16, 2019

What is the purpose of this? As long as the network connections are sorted correctly in settings there is no harm in leaving WIFI on. Maybe to improve battery life?

@opus-nbo

This comment has been minimized.

Copy link

@opus-nbo opus-nbo commented Oct 2, 2019

What is the purpose of this? As long as the network connections are sorted correctly in settings there is no harm in leaving WIFI on. Maybe to improve battery life?

The purpose, for instance is, to free up IP addresses in the local network as clients connected via Wifi AND ethernet use up 2 IPs.

@il0

This comment has been minimized.

Copy link

@il0 il0 commented Oct 23, 2019

at line 36 please change it to:

eth_names=`networksetup -listnetworkserviceorder | sed -En 's|^\(Hardware Port: .*Ethernet, Device: (en.+)\)$|\1|p'`

it will help like me if someone have Ethernet on en10 or higher

This helped me to catch my ethernet adapter, which is en18:

(1) USB 10/100/1000 LAN 8
(Hardware Port: USB 10/100/1000 LAN, Device: en18)
@Calvin-LL

This comment has been minimized.

Copy link

@Calvin-LL Calvin-LL commented Dec 12, 2019

I organized all the fixes and put it here: https://github.com/Calvin-LL/toggleairport
Tested on Catalina

@opus-nbo

This comment has been minimized.

Copy link

@opus-nbo opus-nbo commented Dec 12, 2019

I organized all the fixes and put it here: https://github.com/CoolCyberBrain/toggleairport
Tested on Catalina

(Had to install as admin but not with sudo, our users dont have administrative privileges)

Used it on Mojave on a 2015 MacBook Pro with a Thunderbolt Ethernet Adapter (Interface number below 10) and it does not turn Wifi off, when adapter is plugged in again. When I disable Wifi manually, the "Wired connection detected" notification appears. When unplugging, it always enables Wifi correctly.

@Calvin-LL

This comment has been minimized.

Copy link

@Calvin-LL Calvin-LL commented Dec 13, 2019

I organized all the fixes and put it here: https://github.com/CoolCyberBrain/toggleairport
Tested on Catalina

(Had to install as admin but not with sudo, our users dont have administrative privileges)

Used it on Mojave on a 2015 MacBook Pro with a Thunderbolt Ethernet Adapter (Interface number below 10) and it does not turn Wifi off, when adapter is plugged in again. When I disable Wifi manually, the "Wired connection detected" notification appears. When unplugging, it always enables Wifi correctly.

Interesting, it seems that Apple changes the way the system responds to plugging in and removing ethernet adapters in Catalina. Sadly I don't have a machine on Mojave to test with.

But give this a try:
In the com.mine.toggleairport.plist file in my repo,
Replace

  <key>WatchPaths</key>
  <array>
    <string>/private/var/run/resolv.conf</string>
    <string>/Library/Preferences/SystemConfiguration/NetworkInterfaces.plist</string>
    <string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string>
  </array>

with

  <key>WatchPaths</key>
  <array>
    <string>/Library/Preferences/SystemConfiguration</string>
  </array>

Quick background to the tech savvy, at least in Catalina /Library/Preferences/SystemConfiguration changes all the time, around once a second without any actual input/change/touching the laptop at all which triggers the script to run way too frequently, sometimes leading to non stop notifications (mentioned here) and unable to turn off wifi (multiple instances of the script running at the same time?). So I had to rely on other WatchPaths.

@opus-nbo

This comment has been minimized.

Copy link

@opus-nbo opus-nbo commented Dec 13, 2019

I organized all the fixes and put it here: https://github.com/CoolCyberBrain/toggleairport
Tested on Catalina

(Had to install as admin but not with sudo, our users dont have administrative privileges)
Used it on Mojave on a 2015 MacBook Pro with a Thunderbolt Ethernet Adapter (Interface number below 10) and it does not turn Wifi off, when adapter is plugged in again. When I disable Wifi manually, the "Wired connection detected" notification appears. When unplugging, it always enables Wifi correctly.

Interesting, it seems that Apple changes the way the system responds to plugging in and removing ethernet adapters in Catalina. Sadly I don't have a machine on Mojave to test with.

But give this a try:
In the com.mine.toggleairport.plist file in my repo,
Replace

  <key>WatchPaths</key>
  <array>
    <string>/private/var/run/resolv.conf</string>
    <string>/Library/Preferences/SystemConfiguration/NetworkInterfaces.plist</string>
    <string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string>
  </array>

with

  <key>WatchPaths</key>
  <array>
    <string>/Library/Preferences/SystemConfiguration</string>
  </array>

I have forked your repo and made the changes. Looks like it works now. If you want to make the change in your repo, please go on :)

@Calvin-LL

This comment has been minimized.

Copy link

@Calvin-LL Calvin-LL commented Dec 14, 2019

I organized all the fixes and put it here: https://github.com/CoolCyberBrain/toggleairport
Tested on Catalina

(Had to install as admin but not with sudo, our users dont have administrative privileges)
Used it on Mojave on a 2015 MacBook Pro with a Thunderbolt Ethernet Adapter (Interface number below 10) and it does not turn Wifi off, when adapter is plugged in again. When I disable Wifi manually, the "Wired connection detected" notification appears. When unplugging, it always enables Wifi correctly.

Interesting, it seems that Apple changes the way the system responds to plugging in and removing ethernet adapters in Catalina. Sadly I don't have a machine on Mojave to test with.
But give this a try:
In the com.mine.toggleairport.plist file in my repo,
Replace

  <key>WatchPaths</key>
  <array>
    <string>/private/var/run/resolv.conf</string>
    <string>/Library/Preferences/SystemConfiguration/NetworkInterfaces.plist</string>
    <string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string>
  </array>

with

  <key>WatchPaths</key>
  <array>
    <string>/Library/Preferences/SystemConfiguration</string>
  </array>

I have forked your repo and made the changes. Looks like it works now. If you want to make the change in your repo, please go on :)

Ah thanks for letting me know! I've changed the README to include a pre Catalina version in https://github.com/CoolCyberBrain/toggleairport

@dynek

This comment has been minimized.

Copy link

@dynek dynek commented Jan 8, 2020

Thanks @albertbori & @CoolCyberBrain for that piece of code!

@boesing

This comment has been minimized.

Copy link

@boesing boesing commented Jan 10, 2020

First of all, thanks for this.

I am wondering, if it is possible, to only toggle airport to off when the network configuration of the ethernet port can ping its gateway?
Actually, it instantly turns off when I plug in my docking station. Even tho, OSX did not even start to receive an IP via DHCP.

@mroddewig

This comment has been minimized.

Copy link

@mroddewig mroddewig commented Jan 16, 2020

My ethernet adapter is on a Thunderbolt bridge and the name does not end in "Ethernet" but instead contains "Ethernet" (the full name is "Thunderbolt Ethernet Slot 1").

I edited the regex in the script to look for that instead, my forked gist is at https://gist.github.com/mroddewig/c35c99e1f41c2983dd8dda3eac4ef9d8

@anthonyattard

This comment has been minimized.

Copy link

@anthonyattard anthonyattard commented Jan 20, 2020

What is the purpose of this? As long as the network connections are sorted correctly in settings there is no harm in leaving WIFI on. Maybe to improve battery life?

The purpose, for instance is, to free up IP addresses in the local network as clients connected via Wifi AND ethernet use up 2 IPs.

I see. Thank you for the explanation. Still with 254 ip options on most routers when exactly would this be used?

@albertbori

This comment has been minimized.

Copy link
Owner Author

@albertbori albertbori commented Jan 20, 2020

What is the purpose of this? As long as the network connections are sorted correctly in settings there is no harm in leaving WIFI on. Maybe to improve battery life?

The purpose, for instance is, to free up IP addresses in the local network as clients connected via Wifi AND ethernet use up 2 IPs.

I see. Thank you for the explanation. Still with 254 ip options on most routers when exactly would this be used?

The network sorting is not respected by all applications. Cisco AnyConnect is one of these culprits. Others may have their own reasons for using this.

@Mike377

This comment has been minimized.

Copy link

@Mike377 Mike377 commented Feb 14, 2020

I organized all the fixes and put it here: https://github.com/CoolCyberBrain/toggleairport
Tested on Catalina

Thank everyone on this thread so much for this piece of code. I've been looking around for a solution forever.

@L0VEFIRE

This comment has been minimized.

Copy link

@L0VEFIRE L0VEFIRE commented May 18, 2020

@albertbori -

How do I take this script and change it so instead .... it has the ability to have any connected Wifi network "disconnected" automatically upon connecting (plugging in) Ethernet to your Mac. This way the Mac still has WiFi enabled in order to use "AirDrop," but also is now only using the ethernet connection. (This would be frick'n amazing)

@jakewilliami

This comment has been minimized.

Copy link

@jakewilliami jakewilliami commented May 27, 2020

There's no need to install this as sudo. In fact, registering it as your own user seems to be key to having the notifications actually display (at least on macOS Mojave).

Thank you for this! I couldn't figure out why this wasn't displaying messages.
For those wondering how to do this, just run

chmod u+x /Library/Scripts/toggleAirport.sh
@codymjarrett

This comment has been minimized.

Copy link

@codymjarrett codymjarrett commented Jun 14, 2020

https://github.com/CoolCyberBrain/toggleairport

Thank you for this! It worked perfectly!

@Gabriele-LS

This comment has been minimized.

Copy link

@Gabriele-LS Gabriele-LS commented Jun 24, 2020

I organized all the fixes and put it here: https://github.com/CoolCyberBrain/toggleairport
Tested on Catalina

Thank you so much @CoolCyberBrain

@zdravko455

This comment has been minimized.

Copy link

@zdravko455 zdravko455 commented Sep 18, 2020

Turning off WiFi when LAN is up doesn't work for me. That's on Catalina 10.15.6 and adapter as following:
(1) USB 10/100/1000 LAN
(Hardware Port: USB 10/100/1000 LAN, Device: en7)

(2) Wi-Fi
(Hardware Port: Wi-Fi, Device: en0)

(3) Bluetooth PAN
(Hardware Port: Bluetooth PAN, Device: en6)

Tried to make the matching of the interface more specific but that didn't help.

@bossnass

This comment has been minimized.

Copy link

@bossnass bossnass commented Sep 18, 2020

Turning off WiFi when LAN is up doesn't work for me. That's on Catalina 10.15.6 and adapter as following:
(1) USB 10/100/1000 LAN
(Hardware Port: USB 10/100/1000 LAN, Device: en7)

(2) Wi-Fi
(Hardware Port: Wi-Fi, Device: en0)

(3) Bluetooth PAN
(Hardware Port: Bluetooth PAN, Device: en6)

Tried to make the matching of the interface more specific but that didn't help.

I have the same devices and my regex for the eth_names variable looks like this:

sed -En 's/^\(Hardware Port: (.*Ethernet|USB 10.*), Device: (en.)\)$/\2/p'

This works like a charm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.