Writing scripts for remote actions on Mac
If you need help performing the operations described in this article, please contact your Nexthink Certified Partner.
This article details the process of preparing Nexthink remote action scripts on Mac. The scripts are written in Bash, a command-line shell and scripting language supported by many UNIX-like operating systems, and then signed with a certificate for security purposes. Bash scripts are suited for automating tasks and configuration management; They allow for remote actions to be run on employee devices.
The main use cases for remote actions are on-demand data collection from devices, self-healing tasks and modifying configuration settings.
The article assumes that the reader is familiar with Bash scripting.
Refer to the Remote actions group in Community documentation for more information.
For more information about executing scripts securely, refer to the Remote Actions security best practices on Nexthink Security.
Creating the script
Generic scripts and input variables
Generic scripts are useful when a digitally signed script requires customization, as modifying a signed script breaks its signature. A generic script can be customized by parameters, keeping the signature intact.
Declare parameters at the beginning of a script for Bash positional parameters and enclose them between two special comments as follows:
When uploading the script during the remote action configuration, the system takes the parameters between the special Nexthink comments in a Bash script. It lists them in the Parameters section below the script text. Provide actual values to the parameters in the text input boxes displayed to the right of each parameter name.
Actual values are always passed to the script as text. If the script declares parameters with a type other than string, ensure that you provide values the script can convert to their expected type.
Creating output variables
Executing a script may generate output that you want to store as on-demand data. Nexthink provides a Bash script (nxt_ra_script_output.sh
) that is installed on an employee device at the same time as Collector. The script includes functions to write results to the data layer.
To use the functions in the Nexthink script for remote action output, add the following header at the beginning of your Bash scripts:
All write methods accept two arguments: the name of the output and the value to write. For instance, if you want to return the number of files in a directory to the data layer, the variable nfiles
in your script contains that number. To write the value of nfiles
through output with the name FileNumber
to the data layer, call the function to write unsigned integers:
The Remote Actions editor recognizes calls to write outputs in the script and lists the output variables in the Outputs section. Set the output's label to indicate how to refer to it in investigations and metrics.
The ending of each write
method indicates the type of output. Because Bash is a loosely typed language, the type of output is interpreted depending on the context. The following is a list of available methods:
nxt_write_output_string
0 - 1024 characters (output truncated if bigger)
nxt_write_output_bool
true / false
nxt_write_output_uint32
Min: 0
Max: 4 294 967 295
nxt_write_output_float
Min: -3.4E+38
Max: 3.4E+38
nxt_write_output_size
Min: 0
Max: 3.4E+38
nxt_write_output_ratio
nxt_write_output_bitrate
nxt_write_output_duration
Min: 0 ms
Max: 49 days
Precision in milliseconds
nxt_write_output_date_time
DD.MM.YYYY@HH:MM
nxt_write_output_string_list
0 - 1024 characters (output truncated if bigger)
Implementing campaigns
Combine remote actions with campaigns to help employees solve issues independently. Campaigns let you inform employees about the detection of an issue and guide them through its resolution.
To display a campaign on the desktop of an employee who is interacting with a device:
The campaign must have a Remote action trigger and be published.
The script of the remote action can be executed either:
In the context of the employee, if the action does not need any special privileges.
In the context of the local system account, if the action needs administrative privileges.
Obtaining a campaign identifier
The methods to run a campaign from a remote action require a campaign identifier to be passed as an argument. You can use both the campaign NQL ID (recommended) and the campaign UID (classic option).
To pass the campaign identifier to a remote action, declare a parameter in the script of the remote action for each required campaign. Use the NQL ID (or UID) as the actual value for the parameter when editing the remote action.
For more information on how to get the NQL ID or the UID of a campaign, refer to the Triggering a campaign documentation.
Running a campaign from the script of a remote action
The following functions extend the scripting capabilities of remote actions.
The function runs the campaign matching the NQL ID (preferred) or UID passed as a parameter and saves the answers internally.
The function returns 0 if the campaign status is received.
The function returns 1 in all other cases, and the error is reported in the logs.
Calling the function pauses the execution of the remote action until the employee either completes the campaign or dismisses it.
The function runs the campaign matching the NQL ID (preferred) or UID with the timeout in seconds (0 < T < 1 week) passed as a parameter, and saves the answers internally.
The function returns 0 if the campaign status is received.
The function returns 1 in all other cases, and the error is reported in the logs.
Calling the function pauses the execution of the remote action until the employee either completes the campaign, dismisses it or fails to finalize the campaign before the timeout.
The function runs the campaign matching the NQL ID (preferred) or UID passed as a parameter and saves the answers internally.
The function returns 0 if the campaign status is received.
The function returns 1 in all other cases, and the error is reported in the logs.
Calling the function triggers the start of the campaign and continues the execution of the remote action without waiting for the employee's answers.
The function returns the status of the last campaign.
fully: the employee has fully answered the campaign questions.
timeout: the campaign was timed out before the user finished answering.
postponed: the employee agreed to participate in the campaign.
declined: the employee declined to participate in the campaign.
connectionfailed: the script was unable to connect to the Collector component that controls campaign notifications.
notificationfailed: the script was unable to display the campaign successfully due to one of the following:
The campaign definition could not be retrieved from the platform because of a non-existing campaign or a non-published campaign.
Another campaign is already being displayed to the employee.
A non-urgent campaign cannot be shown due to the focus protection or do-not-disturb rules of Collector. Refer to the Limiting the reception rate of campaigns documentation for more information.
Empty if the last campaign failed.
Query the last campaign using a question label as a string parameter query_key. The function extracts the answer as a string value and saves it in the given variable res_var.
Returns 0 if the campaign status is received.
Returns 1 otherwise.
Encoding the script
To write Bash scripts for remote actions:
Encode the files that contain the script in UTF-8, without BOM.
End each line in the code with the usual character in UNIX systems:
LF
.
Ensure proper encoding to avoid errors or nonfunctioning scripts.
Code examples
Signing the script
For security reasons, Nexthink requires Bash scripts for remote actions on macOS to be digitally signed with the codesign
tool. Apple stores the signature in the extended attributes of a file. Pack your signed script as a tar.gz
file to preserve its extended attributes. Nexthink web interface only accepts files with the tar.gz
extension when importing scripts for remote actions that target macOS.
Nexthink recommends signing all scripts in a production environment. Unsigned scripts should only be used in testing environments.
Creating a signing certificate
Starting from MacOS Sequoia (version 15), instead of deploying the signing certificate itself as trusted on all devices (as is done on Windows), the Root Certificate Authority (CA) of the signing certificate must be deployed due to changes introduced by Apple. Public Root CAs pre-installed with the operating system will not work.
You can deploy one of the following options to all devices where remote actions are executed.
Self-signed Root CA dedicated for Remote Actions
Your corporate Root CA
Below is an example of how to create a self-signed Root CA and use it to generate a code signing certificate for signing remote actions:
Launch Keychain Access on your Mac device.
Go to Keychain Access > Certificate Assistant > Create a Certificate Authority…
Enter the name of your Certificate Authority.
Choose Code Signing for the User Certificate.
Enter an email address (as a point of contact for administrative purposes).
Click Create to generate the certificate.
Below is an example of how to create the code signing certificate itself:
Launch Keychain Access on your Mac device.
Go to Keychain Access > Certificate Assistant > Create a Certificate…
Enter the name of your certificate.
Select the Root CA as the issuer for this certificate.
Click Create to generate the certificate and get a confirmation as shown below.
Once the certificate is created, it is visible in the Keychain Access application. Right-click on the certificate, then click on Get Info.
Scroll down to the bottom and copy-paste the SHA-1 from there. Remove the space from the value to use it for signing the remote action script.
Use the code signing certificate to sign a script:
Check that the previous script signing steps have succeeded by executing the command as shown below.
Packaging
Package the remote action script with a .tar archive and .gzip compression. The extension ".tar.gz" is mandatory.
If the script file is signed, the tar utility will pack its extended attributes as well. This way, the system can transport the code signature and a script file.
Limitations
Only one script can be put into one archive.
A script file should be placed in the root package folder; the
./myscript/myscript.sh
path is incorrect.A script must have the .sh extension.
A script filename must be UTF-8 encoded, which is the default on macOS.
Example
Example of packaging the test.sh
script:
The resulting example_ra_script.tar.gz
is the remote action script file.
Nexthink recommends using this script to simplify the signing and packaging process.
Testing the signed script on the Machine Where the Root CA was generated
When testing a signed script on the same machine where the Root CA was created, you must take the following steps.
Copying the Root CA to the System Keychain and marking it as Trusted
By default, Apple places the generated Root CA in the "login" Keychain. However, you must install it in the "System" Keychain to ensure proper functionality.
To copy the certificate to the System Keychain, follow these steps:
Open Keychain Access.
Export the Root CA certificate by right-clicking it and selecting Export. In the pop-up window, choose the Certificate (.cer) file format.
Open the Terminal application.
Run the following command to import the Root CA into the System Keychain:
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain Certificate.cer
Once imported, verify that the certificate appears in the System Keychain:
Return to Keychain Access.
Select the System section in the left sidebar.
Use the search bar in the top-right corner to locate the certificate.
In the results table, confirm that the Keychain column displays System.
Mark the Root CA certificate as Trusted by right-clicking on it, and selecting Get Info.
In the Trust section, adjust the settings to mark the Root CA as trusted.
Deploying the Root CA of the signing certificate to endpoints
As of MacOS Sequoia version 15, rather than deploying the signing certificate as trusted on all devices like on Windows, the Root Certificate Authority (CA) of the signing certificate must be deployed instead.
Collector Cloud 24.10.2.10 and Collector on-prem 24.10.30.26 support remote action script trusted source validation based on the deployed Root CA as an alternative to the deployed signing certificate.
You can deploy the Root CA using several methods, mainly by creating configuration profiles using JAMF, as described below.
Exporting the certificate created with the Root CA
Choose the Certificate (.cer) file format in the pop-up window.
Deploy to all endpoints using the configuration profile above. The Root CA is marked as trusted automatically.
Verifying that the certificate was properly imported to an endpoint
Copy and unpack your signed remote action script to the endpoint.
Verify your signature.
If the Root CA of the signing certificate is properly imported, you should see the following output:
Maintaining the script
Comparison and validation
Before deploying a remote action script, it is possible to compare it with other scripts prepared by Nexthink. This step is optional but recommended if it is your first time preparing a script.
In the Nexthink Library, select Content.
Filter by Remote action.
Navigate to the Remote Actions management page.
Select any remote action script installed directly from the Nexthink Library that matches the target operating system.
Export the script and compare its syntax with your script’s.
Script termination and timeout
When launching subprocesses from your remote action script and the remote action script terminates or times out, Collector terminates the subprocesses automatically. If you want the subprocess to continue executing after the remote action is terminated, ensure that your script detaches the subprocess using the & character, for example:
Zsh command interpreter
Starting with Collector version 6.27.2, you can run scripts written for the Zsh Unix shell. You must add the following line of code at the very beginning of the shell script:
This is a character sequence known as a shebang (external link).
When the system triggers a remote action on a Mac, Collector checks the first line of code and executes the rest of the instructions using the specified interpreter. A script without a shebang will be executed using the Bash command interpreter.
Nexthink recommends always using a shebang in shell scripts and sticking to the standard interpreters.
Last updated
Was this helpful?