Category Filter

Mac Shell Scripting Resources

With Hexnode UEM, you may remotely execute scripts on devices running macOS 10.11 and above. This document equips you with a collection of shell scripts that Mac admins may find useful in their workflows.

Disclaimer:

The Sample Scripts provided below are adapted from third-party Open-Source sites.

Notes:

To execute custom script on a Mac with Hexnode UEM, you need a script file which can be of the following formats –

Perl (.pl), Bash (.sh), Shell (.sh), C Shell (.csh), Zsh (.zsh), Korn Shell (.ksh), Hypertext Preprocessor (.php), Ruby (.rb), and Python (.py).

For this document, we are using Bash, and the script file should be saved with the extension .sh.

Once you’ve prepared the script, navigate to Manage > Devices > [Find your device], select Actions > Execute Custom Script; this will open a window for you to upload the script file, enter the binary path to run on and the script arguments. Confirm to execute the script.

To view the script output for a device, go to the Action History sub-tab on the device details page and click on Show Output for the script action.

Echo command

The echo command writes arguments to the standard output. When you manually run the script on the Mac Terminal, this will display the output on the Terminal window. When you run the script remotely on the device with Hexnode, you can see the output with the Show Output link under Action History on the device page.

Click on Show output to view echo output

The argument here can be passed as a string –

echo Hello

Or as an argument or wildcard.

Hexnode supports the following wildcards –

  • %devicename%
  • %deviceid%
  • %wifimacaddress%
  • %name%
  • %domain%
  • %email%
  • %imei%
  • %username%
  • %department%
  • %assettag%
  • %devicenotes%

To use wildcards, pass the wildcards as arguments separated by a blank space.

use a single space to separate your arguments

The wildcard will be automatically included in the code as an argument.

Example – echo $1, $2 becomes echo devicename, name

Update OS

The above code uses a combination of commands and tools to update the OS to the latest version of macOS Big Sur available.

softwareupdate –l command is used to fetch the list of all available software updates.

grep “Big Sur” scans the list for available versions of macOS Big Sur.

awk NR==1 filters the updated list to the latest version (row number 1).

cut –d ‘ ‘ -f 3- further processes the output to contain only the OS name identifier.

Finally, we pass the OS name identifier as an argument in softwareupdate -i $getosupd to update the OS to the required version. You may replace “Big Sur” in the above code with an OS version name suitable for your use case.

To list all available OS versions for installation, use the following command –

Copy a text to clipboard

This command will copy a given text to the clipboard of the target system.

Firewall

Turn on Firewall

Turn off Firewall

Notes:

  • 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.