Wifi disabilitare automaticamente quando una connessione Ethernet (cavo) è collegato su un Mac

Panoramica

Questo è uno script bash che trasformerà automaticamente il wifi fuori se ci si connette il computer a una connessione ethernet e wifi sua volta di nuovo quando si scollega il cavo ethernet / adattatore. Se si decide di girare wifi su per qualsiasi motivo, si ricorda che la scelta. Questo è stato improvvisato daquesto mac suggerimento Per lavorare con Yosemite, e senza codificare i nomi degli adattatori. Si suppone che sostenere ringhio, ma non ho controllato che una parte. L'ho fatto, tuttavia, aggiungere il supporto centro di notifica OSX. Sentitevi liberi di forchetta e risolvere qualsiasi problema si verifichi.

La maggior parte del merito di questi cambiamenti vanno aDave Holland.

Istruzioni per l'installazione

  • copiatoggleAirport.sh a/Library/Scripts/
  • Correrechmod 755 /Library/Scripts/toggleAirport.sh
  • copiacom.mine.toggleairport.plist a/Library/LaunchAgents/
  • Correrechmod 600 /Library/LaunchAgents/com.mine.toggleairport.plist
  • Correresudo launchctl load /Library/LaunchAgents/com.mine.toggleairport.plist per iniziare l'osservatore

Istruzioni per disinstallare

  • Correresudo launchctl unload /Library/LaunchAgents/com.mine.toggleairport.plist per fermare l'osservatore
  • Elimina/Library/Scripts/toggleAirport.sh
  • Elimina/Library/LaunchAgents/com.mine.toggleairport.plist
  • Elimina/private/var/tmp/prev_eth_on
  • Elimina/private/var/tmp/prev_air_on

Varie

per eseguire il debug, corri: sudo /Library/Scripts/toggleAirport.sh e aggiungere eco è ovunque vuoi

com.mine.toggleairport.plist

<?xml version ="1.0" encoding ="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Di Apple // DTD PLIST 1.0 // EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<Versione plist ="1.0">
<dict>
  <tasto> Label</tasto>
  <string> com.asb.toggleairport</string>
  <tasto> OnDemand</tasto>
  <true />
  <chiave> ProgramArguments</tasto>
  <matrice>
    <string> /Library/Scripts/toggleAirport.sh</string>
  </matrice>
  <chiave> WatchPaths</tasto>
  <matrice>
    <string> / Library / Preferences / SystemConfiguration</string>
  </matrice>
</dict>
</plist>

toggleAirport.sh

#!/bin / bash

funzione set_airport {

    NEW_STATUS = $ 1

    Se [ $NEW_STATUS = "Su" ]; poi
        / Usr / sbin / networksetup -setairportpower $ air_name su
        touch / var / tmp / prev_air_on
    altro
        / Usr / sbin / networksetup -setairportpower $ air_name off
        Se [ -f "/var / tmp / prev_air_on" ]; poi
            rm / var / tmp / prev_air_on
        fi
    fi

}

funzione di ringhio {

    # Verifica se Growl è installato
    Se [ -f "/usr / local / bin / growlnotify" ]; poi
        / Usr / local / bin / growlnotify -m "$1" -un "AirPort Utility.app"
    altro
        osascript -e "la notifica di visualizzazione \"$1\" con il titolo \"Wifi Toggle " nome suono \"Eroe""
    fi

}

# impostare i valori predefiniti
prev_eth_status ="via"
prev_air_status ="via"
eth_status ="via"

# Afferra i nomi degli adattatori. Assumiamo qui che ogni estremità di nome connessione ethernet in "Ethernet"
Se [ -z $ eth_names ]; poi
   eth_names = $(networksetup -listnetworkserviceorder | grep "Hardware Port" | grep "Belkin USB-C LAN" | awk -F':''{stampare $3}' |  sed -e "S/ //" | sed -e "S/)//")
Elif [ -z $ eth_names ]; poi
   eth_names = $(networksetup -listnetworkserviceorder | grep "Hardware Port" | grep "adattatore Ethernet" | awk -F':''{stampare $3}' |  sed -e "S/ //" | sed -e "S/)//")
fi


# Eth_names = `networksetup -listnetworkserviceorder | Ma è En|^\(Hardware Port: .*adattatore Ethernet, Dispositivo: (a.)\)$|\1|p'`
air_name = `networksetup -listnetworkserviceorder | sed -En 's/^\(Hardware Port: (Wi-Fi|Aeroporto), Dispositivo: (a.)\)$/\2/p'`

# Determinare precedente stato Ethernet
# Se il file esiste prev_eth_on, ethernet era attiva l'ultima volta che abbiamo controllato
Se [ -f "/var / tmp / prev_eth_on" ]; poi
    prev_eth_status ="Su"
fi

# Determinare stesso per stato di AirPort
# Il file è prev_air_on
Se [ -f "/var / tmp / prev_air_on" ]; poi
    prev_air_status ="Su"
fi

# Controllo effettivo stato Ethernet corrente
per eth_name in ${eth_names}; fare
    Se ([ "$eth_name" != "" ] && [ "`Ifconfig $ eth_name | grep "stato: attivo"`" != "" ]); poi
        eth_status ="Su"
    fi
fatto

# E corrente attuale stato di AirPort
air_status = `/ usr / sbin / networksetup -getairportpower $ air_name | awk '{ stampare $4 }'`

# Se ogni cambiamento si è verificato. Esegui script esterno (se esiste)
Se [ "$prev_air_status" != "$air_status" ] || [ "$prev_eth_status" != "$eth_status" ]; poi
    Se [ -f "./statusChanged.sh" ]; poi
        "./statusChanged.sh" "$eth_status" "$air_status" &
    essere
essere

# Determinare se stato Ethernet cambiato
Se [ "$prev_eth_status" != "$eth_status" ]; poi

    Se [ "$eth_status" = "Su" ]; poi
        set_airport "via"
        ringhiare "rete cablata rilevata. Passando AirPort off."
    altro
        set_airport "Su"
        ringhiare "Nessuna rete cablata rilevato. Passando AirPort."
    fi

# Se Ethernet non è cambiata
altro

    # Controllare se lo stato di AirPort cambiato
    # Se così è stato fatto manualmente dall'utente
    Se [ "$prev_air_status" != "$air_status" ]; poi
    set_airport $ air_status

    Se [ "$air_status" = "Su" ]; poi
        ringhiare "AirPort attivata manualmente."
    altro
        ringhiare "AirPort spento manualmente."
    fi

    fi

fi

# Aggiornamento di stato Ethernet
Se [ "$eth_status" == "Su" ]; poi
    touch / var / tmp / prev_eth_on
altro
    Se [ -f "/var / tmp / prev_eth_on" ]; poi
        rm / var / tmp / prev_eth_on
    fi
fi

Uscita 0