Category filter

Script to configure Wi-Fi on Mac

On macOS devices, adding a new Wi-Fi network profile can be done by accessing the Wi-Fi option in the Network preferences. Configuring a single device may seem effortless; however, setting up Wi-Fi on multiple devices can be a tedious process. Additionally, when dealing with remotely managed devices, there needs to be a hassle-free way to configure the Wi-Fi network over the air. This can be especially useful when the passwords or other settings of a corporate network change over time and need to be reflected on the devices. Here’s an easy method to configure Wi-Fi networks on macOS devices using scripts. The script, which can be executed using the ‘Execute Custom Script’ action, allows the seamless setup of Wi-Fi networks on Mac devices remotely.

Scripting language – Bash

File extension – .sh

Disclaimer:


The sample scripts provided below are adapted from third-party open-source sites.

Configure Wi-Fi network

Running this script will automatically connect the device to the configured Wi-Fi network. The user will still be able to connect to other Wi-Fi networks or disable Wi-Fi from the device end. Unlike a Wi-Fi policy, which, when disassociated from the device, disconnects and forgets the configured Wi-Fi, the network configured using this script cannot be remotely disconnected or forgotten.

For example:
networksetup –setairportnetwork en0 ‘Company Wi-Fi' ‘Network@216’

  • networksetup is a command-line tool used to configure a client’s network settings. It includes a variety of support commands to configure various network connections and related settings.
  • The setairportnetwork command is used to configure the Wi-Fi network of the macOS devices.
  • The <Wi-Fi interface name> field value is usually en0. This value can be found out by running networksetup –listallhardwareports command in the Mac terminal. Search for the device name under Hardware Port: Wi-Fi.
  • Replace <network SSID> with the required Wi-Fi network name.
  • Fill the <password> field if the Wi-Fi network is password secured.

What happens at the device end?

Once the script gets successfully executed, the configured Wi-Fi network will be listed under the Known Networks section in the System Settings/Preferences > Network.

The Wi-Fi network listed among the Known Networks after executing the script to setup Wi-Fi

Notes:

  • In Bash script, inserting space while defining network SSID may be treated as an argument separator by the shell interpreter. Insert network SSID and password in single quotes to avoid this.
  • The inputs to be given in the field are case-sensitive. If the SSID is not specified correctly, errors such as ‘network not found’ may appear in the Action History tab as an output, and the device might not connect to the configured network. For example, if the SSID of the network is “OfficeInternet,” the inputs “officeinternet” or “OFFICEINTERNET” might result in an error.
  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository