# Scripts to manage Wi-Fi network settings on macOS devices

The [Execute Custom Script](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/) 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.

Sample script to add a preferred Wi-Fi network on Mac

\#!/bin/sh #!usr/bin/env bash networksetup -addpreferredwirelessnetworkatindex en0 Dex 0 WPA/WPA2 @Spe!jknwjkqf!sd 

   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.

Sample script to join a Wi-Fi network

\#!/bin/sh #!usr/bin/env bash networksetup -setairportnetwork en0 Dex @Spe!jknwjkqf!sd 

   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 `

Sample Script to remove a preferred Wi-Fi network

\#!/bin/sh #!usr/bin/env bash networksetup -removepreferredwirelessnetwork en0 Dex 

   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.

Script to mandate Admin credentials to change Wi-Fi network preferences

\#!/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 

   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.

Script to disable Wi-Fi

\#!/bin/sh #!usr/bin/env bash networksetup -setairportpower en0 off 

   1

2

3

4

5

  \#!/bin/sh 

\#!usr/bin/env bash 

networksetup -setairportpower en0 off 

   

 

  

 Note: 
While you disable Wi-Fi, make sure the device has a connection to other interfaces such as Ethernet. Otherwise, the device loses its connectivity. The Wi-Fi icon on the menu bar will be disabled, and the user cannot turn it on. The wireless network connectivity is disconnected until it is re-enabled.

 

 
To enable Wi-Fi, Script to enable Wi-Fi

\#!/bin/sh #!usr/bin/env bash networksetup -setairportpower en0 on 

   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,

Script to turn off Wi-Fi

\#!/bin/bash networksetup -setnetworkserviceenabled Wi-Fi off 

   1

2

3

  \#!/bin/bash 

networksetup -setnetworkserviceenabled Wi-Fi off 

   

 

  

 Note: 
Executing this script toggles off the Wi-Fi network at the menu bar. However, the user can toggle it on from the device to regain network connectivity.

 

 
To turn on Wi-Fi, Script to turn on Wi-Fi

\#!/bin/bash networksetup -setnetworkserviceenabled Wi-Fi on 

   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.

Script to retrieve the current Wi-Fi network

\#!/bin/bash networksetup -getairportnetwork en0 

   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.

Script to obtain the Wi-Fi network info

\#!/bin/sh #!usr/bin/env bash networksetup -getinfo Wi-Fi 

   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.

Script to identify if Wi-Fi network service is enabled

\#!/bin/bash networksetup -getnetworkserviceenabled Wi-Fi 

   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.

Script to set up search domains for Wi-Fi

\#!/bin/bash networksetup -setsearchdomains Wi-Fi apple.com hexnode.com 

   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

Sample script to set automatic proxy

\#!/bin/bash networksetup -setautoproxyurl Wi-Fi <url> 

   1

2

3

  \#!/bin/bash 

networksetup -setautoproxyurl Wi-Fi <url> 

   

 

  

### Configure domains that can bypass proxy

Sample script to domains that can bypass proxy

\#!/bin/bash networksetup -setproxybypassdomains Wi-Fi Hexnode.com apple.com 

   1

2

3

  \#!/bin/bash 

networksetup -setproxybypassdomains Wi-Fi Hexnode.com apple.com 

   

 

  

 Note: 
You can specify any number of domains. Each of them should be separated using a space.

 

### Set DNS servers for Wi-Fi

Sample script to set DNS servers for Wi-Fi

\#!/bin/bash networksetup –setdnsservers Wi-Fi <dns1> \[dns2\] 

   1

2

3

  \#!/bin/bash 

networksetup –setdnsservers Wi-Fi <dns1> \[dns2\] 

   

 

  

### Set up web proxy

Sample script to set up web proxy

\#!/bin/bash networksetup -setwebproxy Wi-Fi <domain> <port number> <authenticated> <username> <password> 

   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

Sample script to set up web proxy state

\#!/bin/bash networksetup -setwebproxystate Wi-Fi On\[/Off\] 

   1

2

3

  \#!/bin/bash 

networksetup -setwebproxystate Wi-Fi On\[/Off\] 

   

 

  

### Retrieve the web proxy state

Sample script to retrieve the web proxy state

\#!/bin/bash networksetup –getwebproxy Wi-Fi 

   1

2

3

  \#!/bin/bash 

networksetup –getwebproxy Wi-Fi