Created
August 18, 2012 09:46
-
-
Save duhanebel/3385700 to your computer and use it in GitHub Desktop.
Enable/disable wireless on Mountain Lion when eth is connected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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` | |
APNAME="Wi-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 | |
echo "Nothing to do" | |
exit 0; | |
fi | |
# | |
# What software version we are running ? | |
# networksetup syntax changed in Snow Leopard | |
# | |
AP_CMD="/usr/sbin/networksetup -setairportpower ${AIRPORT}" | |
AP_STATUS="/usr/sbin/networksetup -getairportpower ${AIRPORT}" | |
ap_state=`${AP_STATUS} | awk '{ print $NF }'` | |
echo $ap_state | |
# | |
# 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 | |
echo "Turning wi-fi off" | |
${AP_CMD} off | |
fi | |
touch ${beacon} | |
fi | |
exit 0 | |
fi | |
done | |
if [ -f ${beacon} ]; | |
then | |
if [ "${ap_state##* }" == "Off" ]; | |
then | |
echo "Turning wi-fi on" | |
${AP_CMD} on | |
fi | |
rm ${beacon} | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ciao, dove va piazzato e come si usa?