LAN/Wan Switching on a Mac

Jaxson75
New Contributor II

Hi All,

I am not too sure if this is possible, but is there a way to switch between LAN and Wireless connection? Meaning, if I plug in a network cable and connect, the Wireless should not connect anymore. I know that turning off wireless will do it, and we just need it to be automated, or is there an app that can control this on a Mac. We have this option on our HP's that knowns when its on wireless and when it's on LAN. We need to have the same ability on the Mac for one of our Security application in order to report properly.

Any suggests would be helpful....Thank you

10 REPLIES 10

sdagley
Esteemed Contributor II

@Jaxson75 There is no capability to do what you're asking built-in to Jamf Pro. You can write something like that by using a LaunchDaemon to run a script to watch for a wired connection to your corporate network and then turn off Wi-Fi (don't forget you'd want to have it make sure Wi-Fi is turned back on if there's no wired connection active). That said, maybe you should find a better vendor as the Mac will by default favor a wired connection over a Wi-Fi connection and you'd hope a vendor that had a clue could write their software to properly handle that.

Taylor_Armstron
Valued Contributor

FWIW, this has worked fairly well for us (found it here on JamfNation some time ago if I recall)

#!/bin/bash

# Set toggle for found IP on an interface to FALSE to start
IPFOUND=
# Get list of possible wired ethernet interfaces
INTERFACES=`networksetup -listnetworkserviceorder | grep "Hardware Port" | egrep "Ethernet|USB" | awk -F ": " '{print $3}'  | sed 's/)//g'`
INTERFACES=("${INTERFACES[@]}" `networksetup -listnetworkserviceorder | grep "Hardware Port" | grep "Thunderbolt Bridge" | awk -F ": " '{print $3}'  | sed 's/)//g'`)

# Get list of Wireless Interfaces
WIFIINTERFACES=`networksetup -listallhardwareports | tr '
' ' ' | sed -e 's/Hardware Port:/'$'
/g' | grep Wi-Fi | awk '{print $3}'`

# Look for an IP on all Ethernet interfaces.  If found set variable IPFOUND to true.
for INTERFACE in $INTERFACES
do
  # Get Wired LAN IP (If there is one other then the loopback and the self assigned.)
  IPCHECK=`ifconfig $INTERFACE | egrep 'inet [0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' | egrep -v '127.0.0.1|169.254.' | awk '{print $2}'`
  if [ $IPCHECK ]; then
    IPFOUND=true
  fi
done

  if [ $IPFOUND ]; then
    /usr/sbin/networksetup -setairportpower $WIFIINTERFACES off || exit 1
    echo "Turning OFF wireless on card $WIFIINTERFACES."
    logger "wireless.sh: turning off wireless card ($WIFIINTERFACES) because an IP was found on a wired card."
  else
    /usr/sbin/networksetup -setairportpower $WIFIINTERFACES on || exit 1
    echo "Turning ON wireless on card $WIFIINTERFACES."
   logger "wireless.sh: turning on wireless card ($WIFIINTERFACES) because NO IP was found on a wired card."
  fi

Set it in a policy to activate on network state change. Kills wifi within 2-3 seconds of plugging the cable in for us.

Taylor_Armstron
Valued Contributor

(Just looked closer, and FWIW, I have the policy set to run at startup, at network state change, and are regular check-in just to be thorough. Probably overkill, but covers the bases)

Jaxson75
New Contributor II

@Taylor.Armstrong Thank you for the info. I will give it a try and see if this works out on our side.

rseeley
New Contributor III

@Taylor.Armstrong Thank you for posting the script.

I found when setup as a policy, it works great to disable the wifi when ethernet is connected. But since its a policy, the script doesnt have an opportunity to run and enable the wifi again when disconnected. Did you have a solution for this?

T_Armstrong
Contributor

Just tested it in our new office, still seems to be working reliably. I did edit one line.... line 7 in the script to accommodate USB interfaces:

"INTERFACES=("${INTERFACES[@]}" networksetup -listnetworkserviceorder | grep "Hardware Port" | grep "Thunderbolt Bridge" | grep "USB 10/100/1000" awk -F ": " '{print $3}' | sed 's/)//g')"

rseeley
New Contributor III

@Taylor.Armstrong Does the script automatically turn WiFi back on when you disconnect from ethernet? If so how did you set it up? I created ongoing policy that triggers on check-in and network state change. Jamf confirmed that since its a policy and no longer has a network connection once I unplugged, it has no way of running and turning the wifi back on.

T_Armstrong
Contributor

It does... not sure who @ Jamf gave you that info, but that's the whole point of the "make available while offline" checkbox.
71935bc8d4b74e67a73993d5c185f8d4

I believe you'll have to go through at least one checkin cycle first, just so the script gets downloaded to the client, but after that... the policy isn't dependent on the server, so no online access needed for it to run. I'm typing this while on wired connection, now pulling the TB cable out of my laptop and....
9 seconds later by my count, I'm back up and running on wifi. Plug back in and....
18 seconds for me to switch to external monitor, grab DHCP from the dongle plugged into the monitor, and disable wifi.

rseeley
New Contributor III

@Taylor.Armstrong Wow, im surprised I didnt notice that box. But even more surprised that Jamf support didnt point it out. Sure enough, after checking it off, the script worked perfectly. Thank you!

T_Armstrong
Contributor

My pleasure! I don't need to use it often, but when you need it, it is one of the most valuable check-boxes in the entire product :)