Mac Terminal Script to Change Wi-Fi Network

I have two WiFi networks in the new apartment because, for reasons unimportant to this post, I need to use two different internet providers. I use an Elgato Stream Deck to control all kinds of …

I have two WiFi networks in the new apartment because, for reasons unimportant to this post, I need to use two different internet providers. I use an Elgato Stream Deck to control all kinds of automations in the office/studio (Philips Hue light bulbs; Meross, SONOFF, and Shelly in-wall switches, dimmers, and various sensors; and the main Elgato Key Lights). It all connects through Apple HomeKit over Matter for Siri voice and iPhone automation but I have to be connected to a specific network of the two Wi-Fi networks in order for the Stream Deck to be able to “see” the Philips Hue and other WiFi-connected devices locally. (they also work with Alexa and Google Home Assistant but this is an Apple fanboy house)

Standing desk setup with Elgato Key Light and Stream Deck, Genelec speaker monitors, Ninja V, Teleprompter

So I wanted to trigger a script from one of the Stream Deck buttons to switch between the two networks, but Apple doesn’t allow Shortcuts or other automations to do this directly. So, it was time to write a MacOS terminal script and have the Stream Deck execute that when I need to swap networks.

There’s now a specific button that switches between the two networks and I also have it included at the beginning of the main “studio lights on” button multi-action that turns on the Elgato and Philips lights and resets their individual brightness levels and color temperatures to what I expect, just in case they are already on and have been modified manually.

Here’s the script:

#!/bin/bash

# Automatically detect the Wi-Fi interface name
WIFI_INTERFACE=$(networksetup -listallhardwareports | awk '/Wi-Fi|AirPort/{getline; print $NF}')

# Define the network name and password
NETWORK_NAME="ThisNetwork"
NETWORK_PASSWORD="your_password_here"

# Check if the Wi-Fi interface was found
if [ -z "$WIFI_INTERFACE" ]; then
  echo "No Wi-Fi interface found."
  exit 1
fi

# Disconnect from the current network
networksetup -setairportpower "$WIFI_INTERFACE" off
networksetup -setairportpower "$WIFI_INTERFACE" on

# Retry connecting to the network up to 3 times
for i in {1..3}
do
    networksetup -setairportnetwork "$WIFI_INTERFACE" "$NETWORK_NAME" "$NETWORK_PASSWORD"
    if [ $? -eq 0 ]; then
        echo "Connected to $NETWORK_NAME"
        exit 0
    else
        echo "Failed to connect to $NETWORK_NAME, attempt $i of 3"
    fi
    sleep 5
done

echo "Failed to connect to $NETWORK_NAME after 3 attempts."
exit 1

I saved the two versions, one for each network, to my Applications folder, made them executable with

chmod +x change_wifi.sh

and now they can be called like any other application from the Stream Deck.

Terminal script screenshot for Elgato Stream Deck, Key Light, and Philips Hue Bulbs

Here’s the code explanation.

The script starts by detecting the name of the Wi-Fi interface on your Mac using:

WIFI_INTERFACE=$(networksetup -listallhardwareports | awk '/Wi-Fi|AirPort/{getline; print $NF}')

This command searches for the “Wi-Fi” or “AirPort” hardware port and extracts the corresponding interface name (e.g., en0).

Defining the Network Name and Password:

NETWORK_NAME=”ThisNetwork”: The SSID (name) of the Wi-Fi network you want to connect to.

NETWORK_PASSWORD=”your_password_here”: The password for the Wi-Fi network. Replace “your_password_here” with the actual password for the network.

3. Checking if the Wi-Fi Interface is Found:

The script checks if the WIFI_INTERFACE variable is empty. If no interface is found, it exits with an error message.

4. Turning Off and On the Wi-Fi Interface:

To ensure a clean connection attempt, the script turns off the Wi-Fi interface and then turns it back on:

networksetup -setairportpower "$WIFI_INTERFACE" off
networksetup -setairportpower "$WIFI_INTERFACE" on

Connecting to the Wi-Fi Network with Retry Logic:

The script tries to connect to the specified Wi-Fi network up to 3 times using a loop:

for i in {1..3}
do
    networksetup -setairportnetwork "$WIFI_INTERFACE" "$NETWORK_NAME" "$NETWORK_PASSWORD"
    if [ $? -eq 0 ]; then
        echo "Connected to $NETWORK_NAME"
        exit 0
    else
        echo "Failed to connect to $NETWORK_NAME, attempt $i of 3"
    fi
    sleep 5
done

After each attempt, it checks the exit status ($?) of the networksetup command:

  • If the connection is successful (exit status 0), it prints “Connected to ThisNetwork” and exits the script.
  • If the connection fails, it waits for 5 seconds (sleep 5) before trying again.
  • If the connection fails after 3 attempts, the script prints a failure message and exits with an error code.

Just change the network name and the password and it should run just fine for you.

Let me know if this helped you and how you are using it in the comments below.

Jump into the conversation. What's on your mind?