Shell script for turning your macOS laptop's WiFi off and on

Saturday, July 6, 2024 at 5:32 PM | 4 min read

Last modified on Saturday, July 6, 2024 at 5:32 PM

#chmod, #command line, #file permissions, #macOS, #networksetup

WiFi unavailable

Photo by Ann on unsplash.com

Recently I wrote a couple of articles regarding AppleScript and an arrticle regarding integrating an AppleScript into a zsh shell script. Now I talk about creating a shell script in macOS that turns off and on the Wifi on my macOS laptop.

First I create a wifi_reset.sh file inside a folder called macos-scripting located at the root of my home folder (~):

~/ #home folder macos-scripting/ concatenate.sh concatenate.txt download_install_firefox.sh play_album.sh play_song.sh wifi_reset.sh wifi_reset.sh

Next, I add the shebang and path to zsh binary at the top of the file:

#!/bin/zsh

Then I add a set of comments that is sometimes referred to as a script frame or set of commented steps that helps me better think through the code I need to create in each step of the scripting process:

# Reset Wi Fi # by turning it off and on again # turn off WiFi # wait for a few seconds # turn on WiFi

First let's add the code for turning off the WiFi and break it down:

# Reset Wi Fi # by turning it off and on again # turn off WiFi networksetup -setairportpower en0 off # wait for a few seconds # turn on WiFi

networksetup is a macOS built-in tool that controls and modifies network settings from the command line (Terminal). Specifically, it is a configuration tool for controlling network settings located in System Preferences.

The -setairportpower flag sets Wi-Fi power to either on or off. To learn more about networksetup and its available flags, you can run man networksetup in Terminal.

en0 represents the my laptop's hardware port. It follows the -setairportpower flag. However, I need to take another step via Terminal to find out what the value of my WiFi's hardware port is. The syntax for the -setairportpower flag is:

-setairportpower hardwareport on | off

In order to find out what the hardware port of my laptop's WiFi is, I run the following command in Terminal:

networksetup -listallhardwareports

And it returns the following in Terminal:

# The ethernet addresses have been removed Hardware Port: Ethernet Adapter (en4) Device: en4 Ethernet Address: Hardware Port: Ethernet Adapter (en5) Device: en5 Ethernet Address: Hardware Port: Ethernet Adapter (en6) Device: en6 Ethernet Address: Hardware Port: Thunderbolt Bridge Device: bridge0 Ethernet Address: Hardware Port: Wi-Fi Device: en0 Ethernet Address: Hardware Port: Thunderbolt 1 Device: en1 Ethernet Address: Hardware Port: Thunderbolt 2 Device: en2 Ethernet Address: Hardware Port: Thunderbolt 3 Device: en3 Ethernet Address: VLAN Configurations ===================

The Hardware Port: Wi-Fi value, the value of its Device key is en0. So that is what follows networksetup -setairportpower. And since I want to turn off my WiFi, off follows en0.

Next, I'll add the code for turning on my WiFi again:

# Reset Wi Fi # by turning it off and on again # turn off WiFi networksetup -setairportpower en0 off # wait for a few seconds # turn on WiFi networksetup -setairportpower en0 on

I also want to set a delay between turning WiFi off and on again, so that switching it off is actually perceptible:

# Reset Wi Fi # by turning it off and on again # turn off WiFi networksetup -setairportpower en0 off # wait for a few seconds sleep 5 # turn on WiFi networksetup -setairportpower en0 on

The macOS sleep command takes a single argument, which is the length of time that you want to pause before restarting a process suchb as turning on the WiFi again. Here, the length of the pause is 5 seconds.

I can also add feedback in the Terminal while the shell script is running so that I better know what is going on. When is the Wifi turned off and when is it turned on again? So I add the following:

# Reset Wi Fi # by turning it off and on again # turn off WiFi echo "Disabling Wi-Fi" networksetup -setairportpower en0 off # wait for a few seconds sleep 5 # turn on WiFi networksetup -setairportpower en0 on

Now, when I run the ./wifi_reset.sh shell script, the following is returned in TGerminal:

Disabling Wi-Fi Waiting... Re-enabled Wi-Fi

If I removed the echo commands, nothing would be output to Terminal. I would just have to watch the WiFi status menu icon in the Apple menu to see when the icon transforms into a \ (off) and then the WiFi icon again (on).

Note: the echo command outputs text that follows the command. And if you want to output text which consists of multiple lines, you would wrap it in double quotes.

Last but not least, I have to change my shell script file's permissions. If I did not complete this step, by default, I would not be able to run the script:

ls -l wifi_reset.sh -rw-r--r-- 1 mariacam staff 264 Jul 5 14:31 wifi_reset.sh

-rw- represents the permissions of the owner of the file (me). The first - indicates a file, and rw- represents the default permissions of the owner of the file. The owner has read (r) and write permissions, but not execute (x) permissions. So if I try and execute the wifi_reset.sh without changing the default permissions, the following is returned in Terminal:

zsh: permission denied: ./wifi_reset.sh

If I run the following command to change the shell script's file permissions within the macos-scripting folder in Terminal (where the shell script resides):

chmod +x wifi_reset.sh

And then run the ls -l wifi_reset.sh command, the following is returned:

-rwxr-xr-x 1 mariacam staff 264 Jul 5 14:31 wifi_reset.sh

chmod +x automatically adds execute permissions to the owner, groups, and other (users) on macOS. Even though I am the only user on my computer, maybe I still want to be extra careful and only add execute permissions to myself! In that case, I would run the following chmod command instead:

chmod 700 wifi_reset.sh

And when I run the ls -l wifi_reset.sh command, it returns the following in Terminal:

-rwx------ 1 mariacam staff 264 Jul 5 14:31 wifi_reset.sh

Now groups and other (users) don't have the default read (r) anymore. I set all my permissions this way. It's much safer!

Note: the chmod, aka change mode command allows an administrator to set or modify a file's permissions.