0

I have two network environments I want to set up, with one having Wifi enabled but not connected, while the other is connected to a Wifi network. While I can configure whether Wifi is enabled or not, it looks like you can't configure which Wifi network to configure in a network environment.

So the configurations I want to have are:

  • Ethernet only: Ethernet interface enabled with DHCP, Wifi interface enabled but not connected to network. That last part is important and is what I struggle with.
  • Wifi only: Ethernet interface disabled, Wifi enabled and connected to a specific network.

My goal is to be able to quickly change between these two setups. But if I switch between these two environments, I always need to manually connect/disconnect the Wifi network as the environment does not remember my preference here.

Is there a way to do this with macOS tools or third-party helpers?

(System is macOS 12.3 Monterey.)

5
  • This could be scripted with networksetup or you might be able to use the older ControlPlane utility.
    – da4
    Commented Apr 29, 2022 at 14:33
  • @da4: Thanks for the suggestion, using shell tools is certainly a good option for me. But it looks like networksetup does not support disconnecting from a Wifi network, and joining doesn't work on my system, either: Error: -3912 The operation couldn’t be completed. (com.apple.wifi.apple80211API.error error -3912.) (that code corresponds to kCWChallengeFailureErr; it doesn't matter whether I pass the password or let it get it from keychain, unfortunately).
    – DarkDust
    Commented May 2, 2022 at 6:41
  • So the error turned out to be a temporary problem, a reboot fixed it. Still leaves the problem of disconnecting from a Wifi network.
    – DarkDust
    Commented May 2, 2022 at 11:21
  • networksetup -setairportpower <device name> off won't do it?
    – da4
    Commented May 2, 2022 at 14:11
  • 1
    @da: Interesting, -setairportpower off and then on again does indeed not auto-connect to the last connected network and thus seems to do what I want. I didn't try it before since I though it behaves the system settings do. Now I just need to script all that. Thanks a lot for your suggestions!
    – DarkDust
    Commented May 3, 2022 at 7:02

1 Answer 1

0

Thanks to @da4 I now have a working solution.

Switch to Wifi location and join network:

#!/bin/bash

networksetup -switchtolocation "Wifi"

# Get all Wi-Fi interfaces (it should be just one).
WIFI_INTERFACES=`networksetup -listallhardwareports | sed -n '/Wi-Fi/{n;s/^.*:[[:space:]]*//;p;}'`

for INTERFACE in $WIFI_INTERFACES ; do
        networksetup -setairportnetwork $INTERFACE "network" "password"
done

Switch to ethernet location with Wifi interface enabled but not connected to a network:

#!/bin/bash

networksetup -switchtolocation "Ethernet"

# Get all Wi-Fi interfaces (it should be just one).
WIFI_INTERFACES=`networksetup -listallhardwareports | sed -n '/Wi-Fi/{n;s/^.*:[[:space:]]*//;p;}'`

for INTERFACE in $WIFI_INTERFACES ; do
        networksetup -setairportpower $INTERFACE off
        sleep 1
        networksetup -setairportpower $INTERFACE on
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .