Skip to main content
Question

Manaing AirPort via Ethernet connection

  • November 26, 2012
  • 10 replies
  • 28 views

Forum|alt.badge.img+7

Through using a script written by an unknown CCA instructor, we are using a script that will sense when Ethernet is connected and turn AirPort off. When Ethernet is no longer connected, it will turn AirPort back on. This is brilliant. However, I wondered if there were a way to remember the state when Ethernet was connected, and then return it to that state when it was disconnected? This would allow users to turn AirPort off. Currently, when they disconnect Ethernet, AirPort is turned on for them, whether they want it or not.

The script provided has made us heroes in the eyes of our network management as it saves them the overhead of the additional IP leases when users leave AirPort on while connected via wire. The unknown CCA instructor has a beer headed his way when we find him.

10 replies

mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • November 26, 2012

It can probably be done. If its a script, it would just be a matter of having the script write the current state of the Airport/WiFi service to a local file that can be read back into the script when the user disconnects from Ethernet and make a decision on whether to turn power to Airport back on or not.

I don't have any of the specifics offhand on what you'd need to do to make that happen, but when I have a moment, I can look into it. I'm sure someone else will chime in with the details before I get to do that though. :)


mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • November 26, 2012

So just looping around again on this. I assume the script written for you does something to determine the actual Airport device port, as in en0 or en1, depending on the Mac model.

What you'd need to do is use that information in the script to get the state of the WiFi port at the time the script kicks in.

For a MacBook Air or new MacBook Pro's without Ethernet ports:

ifconfig en0 | awk '/status/{ print $2 }'

For other Macs with Ethernet ports:

ifconfig en1 | awk '/status/{ print $2 }'

But the above are just examples, which will return something like "active" or "inactive". Neither should actually be used in the script. it should determine the correct Airport device within the script with whatever variable it uses now.

ifconfig $wifiPort | awk '/status/{ print $2 }'

as an example.

You can make the script write out the current state into a local hidden file, like so:

ifconfig $wifiPort | awk '/status/{ print $2 }' > /path/to/wifiStatus.txt

Somewhere within the script, you will need to check for the existence of that file and, if its there, read back the value into a variable:

if [ -e /path/to/wifiStatus.txt ]; then
    lastWifiState=$(cat /path/to/wifiStatus.txt)
fi

Then, depending on the $lastWifiState returned, either turn WiFi back on or leave it off when the user unplugs Ethernet, using a similar if/then logic (or possibly a case statement, though that wouldn't really be necessary in this instance)

I'll leave the rest up to you to work on. If you have questions or run into issues, just post back. Also keep in mind, this is only one way to skin this cat. There are likely other methods that would work as well.


Forum|alt.badge.img+4
  • Contributor
  • November 27, 2012

@adkinsan We will appreciate if you can share the script?

Regards,
SonuW


Forum|alt.badge.img+21
  • Honored Contributor
  • November 27, 2012

Forum|alt.badge.img+1
  • New Contributor
  • November 27, 2012

if you can't be sure which port (en0, en1) is Airport or Wi-Fi, this snippet could help:

wifiPort=$(networksetup -listallhardwareports | egrep 'Wi-Fi|Airport' -A1 | grep Device | awk '{print $2}')


Forum|alt.badge.img+21
  • Honored Contributor
  • November 28, 2012

Read my link. Guys the search tool is there for a reason. This has already been worked on and worked out ;)


Forum|alt.badge.img+7
  • Author
  • Contributor
  • November 28, 2012

Thanks, tkimpton. As asked by Sonuw, this is the script that we are testing with now:

#!/bin/bash

#############################################
#Some variables to make things easier to read:
#############################################

PlistBuddy=/usr/libexec/PlistBuddy
plist=/Library/Preferences/SystemConfiguration/NetworkInterfaces.plist

#############################################
#Find out how many Interfaces there are
#############################################

count=`networksetup -listallhardwareports | grep Hardware | wc -l | tr -s " "`
echo "Found$count network interfaces"

#############################################
#Get Interfaces
#############################################
#############################################
#reset counter
#############################################
counter=0

while [ $counter -lt $count ] 
do
        interface[$counter]=`$PlistBuddy -c "Print Interfaces:$counter:SCNetworkInterfaceType" $plist` 
    let "counter += 1"
done

#############################################
#Get Real Interfaces
#############################################
#reset counter
#############################################
counter=0

while [ $counter -lt $count ] 
do
        bsdname[$counter]=`$PlistBuddy -c "Print Interfaces:$counter:BSD Name" $plist`
    let "counter += 1"
done

#############################################
#Build Airport Array ${airportArray[@]} and Ethernet Array ${ethernetArray[@]}
#############################################
#reset counter
#############################################
counter=0

while [ $counter -lt $count ] 
do
#############################################
#Check for Airport
#############################################
        if [ "${interface[$counter]}" = "IEEE80211" ]
        then
#############################################
#Add it to the Array
#############################################
            airportArray[$counter]=${bsdname[$counter]}
        fi
#############################################
#Check for Ethernet
#############################################
        if [ "${interface[$counter]}" = "Ethernet" ]
        then
#############################################
#Add it to the Array
#############################################
            ethernetArray[$counter]=${bsdname[$counter]}
        fi
#############################################
    let "counter += 1"
#############################################
done
#############################################


#############################################
#Tell us what was found
#############################################
for i in ${ethernetArray[@]}
do
    echo $i is Ethernet
done

for i in ${airportArray[@]}
do
    echo $i is Airport
done

#############################################
#Check to see if Ethernet is connected
#############################################
#############################################
#Figure out which Interface has activity
#############################################
for i in ${ethernetArray[@]}
    do
    activity=`netstat -I $i | wc -l`
        if [ $activity -gt 1 ]
        then
            echo "$i has activity..."
            checkActive=`ifconfig $i | grep status | cut -d ":" -f2`
#############################################
#Ethernet IS connected
#############################################
            if [ "$checkActive" = " active" ]
            then
                echo "$i is connected...turning off Airport"
#############################################
#Turn off Airport
#############################################
                networksetup -setairportpower ${airportArray[@]} off
                echo "Airport off"
                exit 0
            fi
            if [ "$checkActive" = " inactive" ]
            then
                echo "$i is not active"
            fi
        fi
done
    echo "Checked all Interfaces"


#############################################
#If the script makes it this far assume Ethernet is not connected.
#############################################
#Turn on Airport
#############################################
#
# APL MOD CMN - 11/26/2012 
# Don't turn AirPort back on when disconnected from Ethernet
#
#networksetup -setairportpower ${airportArray[@]} on
#echo "Airport on"
exit 0

Forum|alt.badge.img+7
  • Author
  • Contributor
  • November 28, 2012

As you can see, if the last commands are uncommented, WiFi (or Airport or AirPort) are turned back on when ethernet is disconnected, and flips back on when turned off until ethernet is re-connected.


Forum|alt.badge.img+4
  • Contributor
  • November 28, 2012

@ Tim Kimpton, @ adkinsan Thanks!


Forum|alt.badge.img+21
  • Honored Contributor
  • November 29, 2012

just tried it and doesnt work to stop bridging, which should be the whole point!

I am able to turn on Wi-Fi whilst Ethernet is connect (i have a launch daemon loaded with watchpath of /Library/Preferences/SystemConfiguration)

if you wnt something working look here

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