Category filter
Scripts to manage Wi-Fi network settings on macOS devices
The Execute Custom Script action provides an easy way to push customized settings and configurations to the devices over the air. This document includes different scripts for enabling, managing and modifying Wi-Fi connections and associated network settings for macOS devices.
How to add a preferred Wi-Fi network?
The command networksetup –addpreferredwirelessnetworkatindex adds a given network to the list of preferred networks at the specified index. It takes the following form:
networksetup -addpreferredwirelessnetworkatindex en0 Network_SSID Index_Number Network_Security_Type Network_Password
en0 – Corresponds to the hardware port for Wi-Fi
Network_SSID – Network Name
Index_Number – The preferred network index to be assigned to the network. It can take the integer values 0, 1, 2, etc.
Network_Security_Type – The network security type for the network such as WEP, WPA/WPA2, etc.
Network_Password – Specifies the network password.
1 2 3 4 5 |
#!/bin/sh #!usr/bin/env bash networksetup -addpreferredwirelessnetworkatindex en0 Dex 0 WPA/WPA2 @Spe!jknwjkqf!sd |
How to join a given Wi-Fi network?
You can configure a device to join a given network by embedding the network credentials along with the networksetup command.
networksetup -setairportnetwork en0 Network_SSID Network_Password
If the specified network credentials are correct, the device connects to the given network instantly.
1 2 3 4 5 |
#!/bin/sh #!usr/bin/env bash networksetup -setairportnetwork en0 Dex @Spe!jknwjkqf!sd |
How to remove a preferred Wi-Fi network?
Suppose you want to remove a given network from the list of the preferred networks. You can use the networksetup –removepreferredwirelessnetwork command to do it.
The command takes the following form:
networksetup -removepreferredwirelessnetwork en0 Network_SSID
1 2 3 4 5 |
#!/bin/sh #!usr/bin/env bash networksetup -removepreferredwirelessnetwork en0 Dex |
How to secure Wi-Fi settings?
You can restrict modification of Wi-Fi network settings to Administrator users using airport commands such as
RequireAdminIBSS=YES/NO
RequireAdminPowerToggle=YES/NO
RequireAdminNetworkChange=YES/NO
These commands mandate admin authentication to change the specified Wi-Fi settings on the device. You can modify the parameter values (YES or NO) for these commands to suit your requirements.
1 2 3 4 5 6 7 8 9 |
#!/bin/zsh #!/usr/local/bin/airport sudo ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/local/bin/airport /usr/local/bin/airport -s sudo /usr/local/bin/airport prefs JoinMode=Preferred RequireAdminIBSS=YES RequireAdminPowerToggle=YES RequireAdminNetworkChange=YES |
How to disable/enable Wi-Fi?
You can execute the following script when you want to disable the Wi-Fi network connections to the device completely.
1 2 3 4 5 |
#!/bin/sh #!usr/bin/env bash networksetup -setairportpower en0 off |
To enable Wi-Fi,
1 2 3 4 5 |
#!/bin/sh #!usr/bin/env bash networksetup -setairportpower en0 on |
How to turn on/off Wi-Fi?
To turn off Wi-Fi,
1 2 3 |
#!/bin/bash networksetup -setnetworkserviceenabled Wi-Fi off |
To turn on Wi-Fi,
1 2 3 |
#!/bin/bash networksetup -setnetworkserviceenabled Wi-Fi on |
How to retrieve the name of the current Wi-Fi network connected to the device?
Suppose you want to identify the Wi-Fi network to which the device is connected. The following script helps to retrieve the SSID of the current Wi-Fi network connected to the device.
1 2 3 |
#!/bin/bash networksetup -getairportnetwork en0 |
How to get Wi-Fi network info?
You can obtain the DHCP configuration for the Wi-Fi interface using the following script. It retrieves all the information such as IP address, subnet mask, router, Wi-Fi ID etc.
1 2 3 4 5 |
#!/bin/sh #!usr/bin/env bash networksetup -getinfo Wi-Fi |
How to identify if Wi-Fi network service is enabled?
For a device that can connect to one or more network interfaces, this script helps the admin determine if the Wi-Fi network is enabled.
1 2 3 |
#!/bin/bash networksetup -getnetworkserviceenabled Wi-Fi |
How to set up search domains for the Wi-Fi interface?
Specifying the search domains helps the user to redirect to the websites and servers automatically without entering the complete address.
1 2 3 |
#!/bin/bash networksetup -setsearchdomains Wi-Fi apple.com hexnode.com |
How to configure proxy settings for Wi-Fi?
Proxy server settings that can be configured using scripts include:
Set up automatic proxy
1 2 3 |
#!/bin/bash networksetup -setautoproxyurl Wi-Fi <url> |
Configure domains that can bypass proxy
1 2 3 |
#!/bin/bash networksetup -setproxybypassdomains Wi-Fi Hexnode.com apple.com |
Set DNS servers for Wi-Fi
1 2 3 |
#!/bin/bash networksetup –setdnsservers Wi-Fi <dns1> [dns2] |
Set up web proxy
1 2 3 |
#!/bin/bash networksetup -setwebproxy Wi-Fi <domain> <port number> <authenticated> <username> <password> |
In place of authenticated
, you can specify either ON/OFF to enable or disable authenticated proxy support. You must specify the username and password if authentication is enabled.
Set up web proxy state
1 2 3 |
#!/bin/bash networksetup -setwebproxystate Wi-Fi On[/Off] |
Retrieve the web proxy state
1 2 3 |
#!/bin/bash networksetup –getwebproxy Wi-Fi |