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


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: 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 | # ]