Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Disable AirPort when Ethernet cable is connected Network
At my office, I needed to find a way to turn of the wireless network when someone plugged in their network cable. I also did not want them to be able to turn the wireless network back on until the network cable was unplugged. I came up with the fallowing solution.

I created a launchDaemon called com.companyname.ethernetmonitor, and saved it in /System » Library » LaunchDaemons:
<?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.companyname.ethernetmonitor</string>
  <key>ProgramArguments</key>
  <array>
    <string>/Library/Scripts/CompanyName/turnOffAirport.sh</string>
  </array>
  <key>WatchPaths</key>
  <array>
    <string>/Library/Preferences/SystemConfiguration</string>
  </array>
</dict>
</plist>
This will watch the System Configurations folder for changes. This folder changes when you plug in a network cable, turn on AirPort, etc. When it changes, a script called turnOffAirport.sh, stored in /Library/Scripts, is run to see if the Ethernet connection has a company IP address:
#!/bin/sh
if ifconfig en0 | grep 155.144; 
then /usr/sbin/networksetup -setairportpower off
else
exit 0
fi
This helps keep the user from grabbing two IPs (our wireless and wired network use the same set of IPs), and also helps prevent a few other odd issues when a user is connected to both wireless and Ethernet networks.
    •    
  • Currently 3.57 / 5
  You rated: 5 / 5 (23 votes cast)
 
[33,401 views]  

Disable AirPort when Ethernet cable is connected | 28 comments | Create New Account
Click here to return to the 'Disable AirPort when Ethernet cable is connected' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Disable AirPort when Ethernet cable is connected
Authored by: Coumerelli on Mar 09, '10 07:41:47AM

This is great! I've run into the same need for myself. But when the Ethernet is then unplugged, will it release the airport and then also turn it back on? Doesn't look like it from the script.

---
"The best way to accelerate a PC is 9.8 m/s2"



[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: kainewynd2 on Mar 09, '10 02:41:31PM

NOTE: This command requires the device name, not just the command, so you'll have to add that in as in the examples below.

You could throw a "/usr/sbin/networksetup -setairportpower Airport on" after the else statement like so:

#!/bin/sh
if ifconfig en0 | grep 155.144; 
then /usr/sbin/networksetup -setairportpower Airport off
else
/usr/sbin/networksetup -setairportpower Airport on
fi

Attempting to "Turn On" an already running airport does not result in any change to connectivity in quick testing at home.



[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: ardong on Jul 19, '10 12:31:54PM
I made a small amendment to this script:

#!/bin/sh
if ifconfig en0 | grep inet; 
then /usr/sbin/networksetup -setairportpower Airport off
else
/usr/sbin/networksetup -setairportpower Airport on
fi


[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: hvbarker on Mar 09, '10 07:57:06AM

So why not add a static entry to your dhcpd.conf for both en0 and en1 and use the same IP for both. No need for silly scripts, the interface priority in system preferences -> network will allow en0 to be used when a cable is plugged in, then switch back to en1 when cable is removed.



[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: kclassen on Mar 09, '10 08:12:15AM

If you order the Network connections in the Network Preference to list the Wired connection first, OSX takes care of setting the default interface to the wired network when the cable is connected and dropping back to wireless on disconnect. Works very well for me.



[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: SlaunchaMan on Mar 09, '10 08:15:35AM
That script won't work (at least on Snow Leopard), as you need to specify the adapter. Here's a better version (assuming that your AirPort network is en1):
#!/bin/sh
AIRPORT_INTERFACE="en1"
COMPANY_NETWORK="155.144"
ETHERNET_INTERFACE="en0"

currentNetwork="$(ifconfig ${ETHERNET_INTERFACE} | awk '{ if ( $1 == "inet" ) print $2 }' | cut -d '.' -f 1-2"
currentAirPortPower="$(networksetup -getairportpower ${AIRPORT_INTERFACE} | awk '{ print $NF }')"

if [ "${currentNetwork}" == "${COMPANY_NETWORK}" -a "${currentAirPortPower}" == "On" ]; then
     networksetup -setairportpower ${AIRPORT_INTERFACE} off
elif  [ "${currentNetwork}" != "${COMPANY_NETWORK}" -a "${currentAirPortPower}" == "Off" ]; then
     networksetup -setairportpower ${AIRPORT_INTERFACE} on
fi
Though I haven't tested this script.

[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: gquaggia on Mar 09, '10 08:20:37AM

Great, thank you!



[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: anakin on Mar 09, '10 08:44:56AM

I only have access to 10.6, but this command will not turn off the airport connection.

The command requires that you specify a device.

/usr/sbin/networksetup -setairportpower en1 off



[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: disinfor on Mar 09, '10 08:48:27AM

Awesome!! I actually needed this today! Thanks



[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: diamondsw on Mar 09, '10 09:02:35AM

A very interesting idea - I'll have to try this at home, as I find the priority-based system for using one adapter over another isn't quite foolproof (AFP connections especially seem to "stick" to a particular adapter).

It would be a great improvement if it could also sense when ethernet is unplugged and turn the airport back on. From the looks of it, that shouldn't be too difficult.



[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: zeniam1 on Mar 09, '10 09:18:56AM
I do something similar, but I rely on MarcoPolo to do all of this monitoring for me. Relies on various hardware cues to control what user-defined context to apply. Note that for Snow Leopard, there is a patched version not from the author that properly handles WiFi issues with the current MarcoPolo release. I primarily use it to adjust printer and network location settings based on IP address and SSID presence.

[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: wallybear on Mar 09, '10 09:34:38AM

I would add that you need to edit the line:

if ifconfig en0 | grep 155.144;

replacing the "155.144" with the partial address of your own office network (eg: if your network addresses are like 192.168.0.x, you can replace the "155.144" with "168.0" or "192.168").



[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: hellomrzebra415 on Mar 09, '10 10:27:16AM

You could add it to the script to see if the ethernet dose not have the ip range you are looking for and turn the airport back on, but i prefer to leave it off as the airport network gets saved and will reconnect when airport is turned back on. I only want the users to use the network when really needed. Also keeping the airport off can help speed up login and wake from sleep as it dose not take time to connect to the network and try to check the password since the computers are bound to AD it will use the mobile account.



[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: Bassman59 on Mar 09, '10 11:35:58AM

I find it a LOT easier to take advantage of the network "Locations" feature.

I have four locations: Airport (my network at home, over wireless, with static IP address), Hardwire Home (my network at home, hardwired, with static IP address), Wireless DHCP (should be obvious) and Hardwire DHCP (ditto). In each case, only one of the possible network interfaces is active.

So rather than rely on a script or a daemon to monitor the ports, I just choose the appropriate network configuration.



[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: zebrum on Mar 09, '10 11:39:17AM

well you didn't say why....but in my mind you don't need to do this. All you do is set the network order in network prefs so that ethernet takes priority over airport. Then you can happily just leave the airport on all the time.



[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: allanmarcus on Mar 09, '10 12:20:17PM
You need to change the script a bit to work with 10.6. This works for 10.5 and 10.6. If you need 10.4, you need to modify it.

if [ `sw_vers -productVersion | grep 10.5` ]; then
	networksetup -setairportpower off
else
	networksetup -setairportpower 'AirPort' off
fi


[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: prometheon123 on Mar 09, '10 12:25:35PM

Fantastic script!

One amendment: On Snow Leopard the command is /usr/sbin/networksetup -setnetworkserviceenabled "AirPort" off



[ Reply to This | # ]
?
Authored by: slb on Mar 09, '10 12:33:50PM

Question: should this be saved as a ".plist"? All other LaunchDaemons are such.



[ Reply to This | # ]
?
Authored by: kainewynd2 on Mar 09, '10 02:33:48PM

Yes, save it as a .plist.

Additionally, you should move it to /Library/LaunchDaemons instead of /System. System is generally reserved for Apple... /Library/LaunchDaemons should work fine.



[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: Crazor on Mar 09, '10 12:47:42PM

I'd suggest grepping for 155.144. or even better for " 155.144." or even "inet 155.144." to avoid activation of the script when assigned an IP like "169.254.155.144" or similar. This would lead to unexpected behaviour ;)

Other than saving IPs, what other "odd issues" do you experience? I use airport and ethernet all the time at home or on campus. I prioritized the ethernet interface higher than airport in system configuration, and this works pretty seamless. For example, when I copy a big file from another machine via airport, I can plug in the ethernet cable and after a few seconds, the transfer speed increases from the slow wireless connection to super-fast gigabit ethernet ;)



[ Reply to This | # ]
Another Take
Authored by: joelbruner on Mar 09, '10 01:21:10PM
Here's another take on this, it checks to see if en0 and en1 are using the same DHCP server, a surer way to tell if you are duplicating a network connection. Then gives user time to unplug the cable, this way they can turn on wireless and maintain connection so Finder won't freak out. Also Geeklog or whatever blog system this is seems to want to mangle the \n newline in display dialog for some reason...
#!/bin/bash
scriptName=$(basename $0)

function getDHCPservers { en0=$(ipconfig getpacket en0 | grep server_identifier | awk {'print $3'}) en1=$(ipconfig getpacket en1 | grep server_identifier | awk {'print $3'}) }
function sendlog { logger -t "$scriptName [$$]" ${1} }
#just in case the values aren't flushed after network transition sendlog "Network Transition Detected" #give it a moment to get values sleep 5 getDHCPservers;
if [ -z "$en0" -o -z "$en1" ]; then sendlog "No DHCP Server Redundancy: en0=$en0 en1=$en1" exit; fi
if [ "$en0" == "$en1" ]; then sendlog "DHCP Server Redundancy: en0=$en0 en1=$en1" sendlog "Waiting 30 Seconds for User to Disconnect Ethernet" sleep 30 getDHCPservers;
#check again if [ "$en0" == "$en1" ]; then sendlog "Duplicate DHCP Server persist: en0=$en0 en1=$en1" sudo networksetup -setairportpower Airport off sendlog "Airport Turned Off" open /System/Library/CoreServices/Menu\ Extras/AirPort.menu osascript <<-EOF tell application "Finder" display dialog "Duplicate Net Connections Found&#92;nAirport Turned Off" buttons { "OK" } with icon 2 giving up after 15 end tell EOF fi else sendlog "No DHCP Server Redundancy" fi

[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: hellomrzebra415 on Mar 09, '10 02:02:54PM

yes it should have the .plist



[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: jyu on Mar 10, '10 12:51:51AM
It's a nice idea. But depend on the sequence, it may not accomplish this: "This helps keep the user from grabbing two IPs."

Here is the logic:

- If you turn on or wait up the laptop after plug in Ethernet cable, then this script may work;
- But if you turn on or wait up the laptop first, Airport should've already grabbed an IP from your DHCP server. Even if you turn Airport off later, that IP won't be available to other devices until it's expired.

[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: tangential on Mar 10, '10 05:23:45AM

I use Marco Polo (which is free) to do this. Its pretty simple to set up a context that accomplishes this.



[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: tom larkin on Mar 15, '10 09:25:37AM

What exactly is stopping the user from turning the Airport back on though? If your list of order for network services lists Ethernet a higher priority it will use it over anything else. The only time I see this eventful is if you want to save battery power on a mobile device, but then you have the overhead of launchd running and checking every so often, or if you want to save unneeded IP addresses on your wireless VLANs.

If you enable admin rights to modify the airport card, then you have users that will need admin passwords every time they want to join a network, which would defeat the purpose.



[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: yurykk on Jul 15, '10 02:14:12AM
I made some changes to the script -

1) Will work on both Leopard and Snow Leopard
2) It will watch for ALL Ethernet and Bluetooth interfaces (discovered automatically)
3) AirPort will be automatically turned ON if all Ethernet and Bluetooth PAN are disconnected
4) AirPort will be automatically turned OFF once one of the Ethernet interfaces or Bluetooth PAN gets connected (in "active" state)

If you want to monitor IP address assignment instead of just "state" - uncomment line # 62 and comment line # 67

#!/bin/bash

AIRPORT=""
ALLINTERFACES=""

IFS='
'

#
# Look for AirPort interface and Create list of watched network interfaces
# We are looking for all Ethernet interfaces and Bluetooth PAN
#

for intf in `/usr/sbin/networksetup -listnetworkserviceorder | grep "^(H"`
do
   IFS=':,)'
   set $intf
   if [[ ($2 =~ Ethernet ) || ( $2 =~ "Bluetooth PAN" ) ]];
   then
      ALLINTERFACES="${ALLINTERFACES} $4";
   fi
   if [[ ($2 =~ AirPort ) ]]; then AIRPORT=$4; fi
done

IFS=' 	
'

#
# If no interfaces to watch or no AirPort found - do nothing
#

if ( ([ -z "${ALLINTERFACES}" ]) || ([ -z ${AIRPORT} ]) );
then
  exit 0;
fi

#
# What software version we are running ?
# networksetup syntax changed in Snow Leopard
#

SW_VER=`/usr/bin/sw_vers -productVersion`

if [ `echo "if(${SW_VER%.*}>=10.6)r=1;r"|/usr/bin/bc` -eq 1 ];
then
   AP_CMD="/usr/sbin/networksetup -setairportpower ${AIRPORT}"
else
   AP_CMD="/usr/sbin/networksetup -setairportpower"
fi


#
# Check if watched interface have IP address assigned
# or (as an alternative - check if the interface is connected or not)
#

for ethintf in ${ALLINTERFACES}
do

   # Check if IPv4 address is assigned
   # 
   # ifconfig ${ethintf} 2>/dev/null | grep "inet " > /dev/null

   #
   # Check if interface is active
   #
   ifconfig ${ethintf} 2>/dev/null | grep "status: active" > /dev/null

   assigned=$?

   if [ $assigned -eq 0 ];
   then
      ${AP_CMD} off
      exit 0
   fi
done

${AP_CMD} on
exit 0


[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: ardong on Jul 19, '10 12:28:30PM
Thanks to the original poster and kainewynd2 for their suggestions. The scripts work like a champ! I'm running 10.6.3 on a 2008 Macbook.

[ Reply to This | # ]
Disable AirPort when Ethernet cable is connected
Authored by: yurykk on Sep 03, '11 03:37:40AM
Fix. Now works with Lion.
############
#!/bin/bash

#
# This script will turn ON AirPort only if it was turned OFF before
# by this script.
# If AirPort was turned off manually, it will not be automatically enabled
#

AIRPORT=""
ALLINTERFACES=""

beacon=/var/log/AirPortBeacon.beacon

IFS='
'

SW_VER=`/usr/bin/sw_vers -productVersion`

if [ `echo "if(${SW_VER%.*}>=10.7)r=1;r"|/usr/bin/bc` -eq 1 ];
then
   APNAME="Wi-Fi"
else
   APNAME="AirPort"
fi

#
# Look for AirPort interface and Create list of watched network interfaces
# We are looking for all Ethernet interfaces and Bluetooth PAN
#

for intf in `/usr/sbin/networksetup -listnetworkserviceorder | grep "^(H"`
do
   IFS=':,)'
   set $intf
   if [[ ($2 =~ Ethernet ) || ( $2 =~ "Bluetooth PAN" ) ]];
   then
      ALLINTERFACES="${ALLINTERFACES} $4";
   fi
   if [[ ($2 =~ ${APNAME} ) ]]; then AIRPORT=$4; fi
done

IFS=' 	
'

#
# If no interfaces to watch or no AirPort found - do nothing
#

if ( ([ -z "${ALLINTERFACES}" ]) || ([ -z ${AIRPORT} ]) );
then
  exit 0;
fi

#
# What software version we are running ?
# networksetup syntax changed in Snow Leopard
#

if [ `echo "if(${SW_VER%.*}>=10.6)r=1;r"|/usr/bin/bc` -eq 1 ];
then
   AP_CMD="/usr/sbin/networksetup -setairportpower ${AIRPORT}"
   AP_STATUS="/usr/sbin/networksetup -getairportpower ${AIRPORT}"
else
   AP_CMD="/usr/sbin/networksetup -setairportpower"
   AP_STATUS="/usr/sbin/networksetup -getairportpower"
fi

ap_state=`${AP_STATUS}`

#
# Check if watched interface have IP address assigned
# or (as an alternative - check if the interface is connected or not)
#

for ethintf in ${ALLINTERFACES}
do

   # Check if IPv4 address is assigned
   # 
   # ifconfig ${ethintf} 2>/dev/null | grep "inet " > /dev/null

   #
   # Check if interface is active
   #
   ifconfig ${ethintf} 2>/dev/null | grep "status: active" > /dev/null

   assigned=$?

   if [ $assigned -eq 0 ];
   then
      if [ "${ap_state##* }" == "On" ];
      then
	if [ ! -f ${beacon} ];
	then
           ${AP_CMD} off
	fi
         touch ${beacon}
      fi
      exit 0
   fi
done

if [ -f ${beacon} ];
then
  if [ "${ap_state##* }" == "Off" ];
  then
    ${AP_CMD} on
  fi
    rm ${beacon}
fi
exit 0



[ Reply to This | # ]