Disable Airport when Ethernet is active

ericbenfer
Contributor III

I would like to somehow automatically disable Airport when Ethernet is the active network connection. And potentially re-enable it when Ethernet is not in use.
I already have all my Macs configure with Ethernet at the top of the network service order.
In English the script would...
When a network change is detected, check to see if Ethernet is in use.
If Ethernet is in use turn the power off to Airport.
Else turn Airport on (or don’t do anything to Airport)
I know the command to turn the power off (/usr/sbin/networksetup -setairportpower off), but I am drawing a blank for how the detect the network change.

Do you think Casper can detect the network change, or is some LaunchD needed.
--

Eric Benfer
ITSD – Macintosh Services Manager
Johns Hopkins University Applied Physics Laboratory
11100 Johns Hopkins Rd
Laurel, MD 20723
eric.benfer at jhuapl.edu
443-778-4248 Balto.
240-228-4248 DC

16 REPLIES 16

John_Wetter
Release Candidate Programs Tester

You could use the state change pkg in the Resource Kit to detect the change and have it run the command to change back and forth. Just curious though, if the order is right, why disable the airport?

John

--
John Wetter
Technical Services Manager
Educational Technology, Media & Information Services
Hopkins Public Schools
952-988-5373<livecall:952-988-5373>

Not applicable

There was recently a MacOSXhints.com hint about this. I have implemented this on my machine and I love it. I personally had issues running iChat, or any other program that had an open connection when I plugged in the cable, as all the existing traffic would continue going through the airport, rather than the faster ethernet cable. I also noticed that Bonjour would act more reliable when only one connection was enabled.

Here's the hint (http://www.macosxhints.com/article.php?story100305114751547&query=disable+airport ). I had to modify the script a bit, so I'll put my modifications here as well.

#!/bin/sh

if ifconfig en0 | grep inet;

then /usr/sbin/networksetup -setairportpower en1 off

else
/usr/sbin/networksetup -setairportpower en1 on
fi
I changed it to turn the airport back on, and to turn the airport off if it detected the ethernet port had any IP address (could be a problem with a 169 address). I did not deploy this via Casper, but you could add the launchd item, and the script, but then it would not be controlled via Casper.

HTH,
Robert

tlarkin
Honored Contributor

What is stopping the user from turning it back on?

abenedict
New Contributor II

I believe that if you did it as a launchd item, that you could have launchd
trigger the script any time a specific file is touched, so if a user were to
turn the airport back on the script would immediately run and turn it back
off. If anyone knows the file that gets touched when changing a network
interface, I would be glad to hear which one. We have our wired and wireless
network assigning addresses from the same subnet and it would be nice to
keep the addresses handed out to a minimum as we don't want to create a
separate subnet for wireless.

--
Alan Benedict
?
Macintosh Technician
The Integer Group
O: 515-247-2738
C: 515-770-8234
http://www.integer.com

pickerin
Contributor II

Here is my script for doing this. I put it in my Every15 and am currently testing it. Seems to work fine so far. It does not toggle the AirPort back on and may not work for older OSX versions than say 10.7:

#!/bin/sh
OS=`/usr/bin/sw_vers -productVersion | /usr/bin/colrm 5`
ACTIVEIF=`/usr/sbin/netstat -rn 2>&1 | /usr/bin/grep -m 1 'default' | /usr/bin/awk '{ print $6 }'`
WIFIIF=`/usr/sbin/networksetup -listallhardwareports | grep -A 1 Wi-Fi | awk '/Device/{ print $2 }'`
WIFIPWR=`/usr/sbin/networksetup -getairportpower $WIFIIF | sed 's/Wi-Fi Power.*: //g'`

if [ "$ACTIVEIF" == "$WIFIIF" ]; then
        exit 0
else
        if [ "$WIFIPWR" != "On" ]; then
                exit 0
        else
                /usr/sbin/networksetup -setairportpower $WIFIIF off
                /usr/sbin/jamf displayMessage -message "Your wireless network adapter has been disabled as you are connected via Ethernet.  You will need to enable the Wi-Fi when disconnected from the Ethernet network."
        fi
fi

Not applicable

For environments using various models with various 'en' labels, I've developed the following script to disable WiFi when an ethernet is detected and enable after ethernet is removed.

#!/bin/bash

SAFEIFS=$IFS
IFS="
"
wifiDevice=`networksetup -listallhardwareports | grep 'Wi-Fi' -A1 | grep -o en.`
wifiStatus=`ifconfig "$wifiDevice" | grep 'status' | awk '{ print $2 }'`
ethernetDevice=`networksetup -listallhardwareports | grep 'en' | grep -v "$wifiDevice" | grep -o en.`
ethernetStatus=`for i1 in ${ethernetDevice};do
    echo $(ifconfig "$i1" | grep 'inet' | grep -v '127.0.0.1|169.254.' | awk '{ print $2 }')
done`
if [ "$ethernetStatus" ] && [ "$wifiStatus" = "active" ]; then
    $(networksetup -setairportpower "$wifiDevice" off)
    touch /var/tmp/wifiDisabled; fi
if [ "$ethernetStatus" = "" ] && [ "$wifiStatus" = "inactive" ]; then
    if [ -f "/var/tmp/wifiDisabled" ]; then
        rm -f /var/tmp/wifiDisabled
        $(networksetup -setairportpower "$wifiDevice" on); fi
fi
IFS=$SAVEIFS
exit 0

Use a launch agent or daemon to run at load and keep alive.

tkimpton
Valued Contributor II

Please check this out. Quite a few of us worked on this to perfect it. It's not mine it's ours :)

https://jamfnation.jamfsoftware.com/discussion.html?id=5327

Not applicable

Modified to also audit AirPort power. Script now disables wireless interface even if not connected to an SSID.

#!/bin/bash

##
# Define wireless interface "en" label.
wifiInterface=$(networksetup -listallhardwareports | grep 'Wi-Fi' -A1 | grep -o en.)
##
# Define wireless interface active status.
wifiStatus=$(ifconfig "${wifiInterface}" | grep 'status' | awk '{ print $2 }')
##
# Define wireless interface power status
wifiPower=$(networksetup -getairportpower "${wifiInterface}" | awk '{ print $4 }')
##
# Define non-wireless interface "en" labels.
ethernetInterface=$(networksetup -listallhardwareports | grep 'en' | grep -v "${wifiInterface}" | grep -o en.)
##
# Define non-wireless IP status.
ethernetIP=$(for i1 in ${ethernetInterface};do
  echo $(ifconfig "${i1}" | grep 'inet' | grep -v '127.0.|169.254.' | awk '{ print $2 }')
done)
##
# Disable active wireless interface if non-wireless interface connected.
if [[ "${ethernetIP}" && "${wifiStatus}" = "active" ]] || [[ "${ethernetIP}" && "${wifiPower}" = "On" ]]; then
  networksetup -setairportpower "${wifiInterface}" off
  touch /var/tmp/wifiDisabled; fi
##
# Enable inactive wireless interface if previously disabled by daemon.
if [[ "${ethernetIP}" = "" && "${wifiStatus}" = "inactive" ]] || [[ "${ethernetIP}" = "" && "${wifiPower}" = "Off" ]]; then
  if [[ -f "/var/tmp/wifiDisabled" ]]; then
    rm -f /var/tmp/wifiDisabled
    networksetup -setairportpower "${wifiInterface}" on; fi
fi
##
# Sleep to prevent launchd interpreting as a crashed process.
sleep 10
exit 0

Script is initiated by following Launch Daemon

<?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.company.auditwifi</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/company/bin/audit_wifi</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StartInterval</key>
    <integer>15</integer>
    <key>UserName</key>
    <string>root</string>
</dict>
</plist>

designbridge
New Contributor II

Hi,

thanks for the script it works well until one of our users plugs in their iPhone. Is there away of excluding iPhonesiPads so that if they plug in WiFi will stay active?

thanks,

As

ammonsc
Contributor II

@designbridge I am having the same issue with the iPhone disconnects. Any idea how to fix that? Did you get it figured out?

ammonsc
Contributor II

I added the following

# Define iPhone interface "en" label.
iphoneInterface=$(networksetup -listallhardwareports | grep 'iPhone' -A1 | grep -o en.)

And then modified this line to add the iPhone interface

# Define non-wireless interface "en" labels.
ethernetInterface=$(networksetup -listallhardwareports | grep 'en' | grep -v "${wifiInterface},${iphoneInterface}" | grep -o en.)

That seemed to do the trick

claudiogardini
Contributor

fyi Under "Client-Side-Restrictions" there is the option to only run the Policy if Ethernet is available. I just have a simple Script to turn of WiFi and i'm running that with said restriction.

09bba44e8f3542e7bce830b65802a65e

Taylor_Armstron
Valued Contributor

@claudiogardini ..... that's beautifully simple.

I can't believe I hadn't thought of that.

What about turning it back on?

ammonsc
Contributor II

@claudiogardini what a great idea! I second @Taylor.Armstrong how do you turn it back on?

nimitz
New Contributor II

@ammonsc would it be possible to have another script to turn wifi on if there were no network connections?

wasabi
New Contributor

Just a suggestion, we have used in the past an app named "lingon" https://itunes.apple.com/ca/app/lingon-3/id450201424?mt=12
We used an older version. Not sure about the current app. It allowed us to easily set up cron jobs, kill and start processes etc..