Désactivation auto du wifi quand ethernet est utilisé (et inversement) ?

d0.0b

Membre enregistré
13 Octobre 2019
4
0
53
Hello,
J'aimerais que le wifi de mon MBP se désactive automatiquement quand je branche mon câble ethernet (*) et se réactive automatiquement quand je débranche le câble.

Y aurait-t-il un moyen (via terminal, un script…) de faire ça ?

En effet j'ai remarqué qu'après avoir branché mon câble ethernet, et bien que cette connexion soit prioritaire (classée en 1er dans Réseau), les applis ouvertes avant restent "bloquées" sur le wifi qui est toujours actif.
Et pour qu'elles utilisent ethernet je dois désactiver manuellement le wifi ou les quitter/relancer.

Merci

(*) : on pourrait aussi imaginer qu'après désactivation, le wifi se réactive automatiquement dès que la connexion ethernet est établie (afin de profiter de la localisation qui exige l'activation du wifi par exemple). À voir si cela si cela résout mon problème quand même.
Ça rendrait du coup la 2ème action (débranchement/réactivation) inutile.
 
Hello, après quelques recherches, voici, pour ceux que ça intéresse, le script "airport2.5.sh" que j'ai trouvé sur internet (et légèrement modifié pour que le wifi se reconnecte automatiquement après avoir débranché ethernet), ainsi que le launchdeamon "com.tsys.airport2.5.plist" correspondant.
Fonctionne avec Catalina (et OS précédent probablement)
En revanche, impossible de connecter le wifi manuellement, une fois ethernet connecté, pour le moment…

SCRIPT :
-----------

#!/bin/bash
#################################################
# Script originally from the posted
# "Air Defense" script posted awhile back on JAMF Nation
# Revised to work on newer USB-C hardware Macs
# Revision by Christopher Miller for
# ITSD-ISS of JHU-APL, Dated: 2017-02-20
#################################################
# Some variables to make things easier to read:
#############################################
PlistBuddy=/usr/libexec/PlistBuddy
plist=/Library/Preferences/SystemConfiguration/NetworkInterfaces.plist
FILE=/tmp/ether.cfg


# Find the number of Interfaces
######################################################
# NOTE: Newer USB-C only systems keep reporting
# the Thunderbolt Bridge adapter as ALWAYS active
# Thus using grep -v to avoid listing the bridge
######################################################
count=$(networksetup -listallhardwareports | grep -i "Device: en" -B 1 | grep -v -i "Bridge" | grep -v -i "Bluetooth"| grep "Hardware" | grep -v -i "fw" | wc -l | tr -s " ")
#count=$(networksetup -listallhardwareports | grep -i "Device: en" -B 1 | wc -l | tr -s " ")
echo "Found$count network interfaces"
let count=count+1


# Set Counter to Zero, Get Interface media
#############################################
counter=0
while [ $counter -lt $count ]
do
interface[$counter]=$($PlistBuddy -c "Print Interfaces:$counter:SCNetworkInterfaceType" $plist)
#echo $interface[$counter]
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)
echo $bsdname[$counter]
let "counter += 1"
done


##########################################################################################
# Build Airport Array ${airportArray[@]} and Ethernet Array ${ethernetArray[@]}
##########################################################################################
counter=0
while [ $counter -lt $count ]
do
# Check for Airport, add to array when found
if [ "${interface[$counter]}" = "IEEE80211" ]; then
airportArray[$counter]=${bsdname[$counter]}
fi

# Check for Ethernet, add to array when found
if [ "${interface[$counter]}" = "Ethernet" ]; then
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

############################################
# Add interfaces from ifconfig rather than
# using the standard loop above
############################################


ifconfig |grep en[1-9]: |awk '{print $1}'| cut -d '=' -f 2 | sed 's/:$//' >>$FILE

getArray() {
array=() # Create array
while IFS= read -r line # Read a line
do
array_en+=("$line") # Append line to the array
done < "$1"
}

getArray "$FILE"


for i in "${array_en[@]}"; do echo "$i"; done


#############################################
# Check to see if any Ethernet is connected
# Figure out which Interface has activity
#############################################
#MACTST=`ifconfig | grep "ac:de:48:00:11:22" |awk '{print $2}'`
mycount=`ifconfig |grep en[1-9]: |wc -l`
#for i in ${ifaces[@]}
for i in ${array_en[@]}
do
MACTST=`ifconfig | grep "ac:de:48:00:11:22" |awk '{print $2}'`
MACADD=`ifconfig $i| grep ether |awk '{print $2}'`
checkActive=`ifconfig $i | grep status | awk '{print $2}'`

echo $i $MACADD $checkActive
echo "Are these the same $MACTST $MACADD?"
if [ "$MACADD" == "$MACTST" ]; then
echo "They are the same!"
checkActive="inactive"
else
echo "$i is not the toolbar bus."
fi
if [ "$checkActive" == 'active' ]; then
# Ethernet IS connected
echo "$i is connected with MAC address $MACADD...turning off Airport"
networksetup -setairportpower ${airportArray[@]} off
echo "Airport off"
else
# Ethernet is NOT connected
networksetup -setairportpower ${airportArray[@]} on
echo "$i is not active"
fi

done
rm $FILE
echo "Checked all Interfaces"

# Exit the script, we'll check again later
exit 0
----------------
FIN SCRIPT

LAUNCHDEAMON :
-------------------------

<?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>airport2</string>
<key>LingonWhat</key>
<string>/Library/Scripts/airport2.sh</string>
<key>ProgramArguments</key>
<array>
<string>/Library/Scripts/airport2.5.sh</string>
</array>
<key>WatchPaths</key>
<array>
<string>/Library/Preferences/SystemConfiguration/</string>
</array>
</dict>
</plist>
-------------------------------
FIN LAUNCHDEAMON
 
networksetup -setairportpower ${airportArray[@]} on

Oops, je n'y connais rien en script et il s'avère que ma petite modif à la fin du script initial (ci-dessus) provoque un comportement malencontreux : le wifi se connecte et déconnecte dans la foulée toutes les 30 secondes environ, quand ethernet est connecté…

Bref, voici un autre script que je viens de trouver et qui semble faire le job sans problème pour le moment : https://github.com/paulbhart/toggleairport
Celui-ci permet d'ailleurs de reconnecter le wifi manuellement si besoin.