Category filter

How to create and execute custom scripts on Linux?

A script is a set of commands written in a scripting language that automates repetitive tasks, thereby reducing the need for significant manual intervention by the admin. Scripting plays a key factor for process automation in Linux and it involves the creation of executable text files with commands written in shell languages such as bash, csh, or ksh. Various routine administrative tasks can be deployed in Linux using scripts, such as user management, system monitoring, software updates, and so forth.

While Hexnode offers a range of built-in features, it also allows possibilities for customized functionalities based on organization’s requirements. With the Execute Custom Script action, administrators can deploy scripts across devices from a centralized console, permitting scaled-up management capabilities. To execute custom scripts, the required configurations must first be compiled into a script file, which is then deployed from the UEM console for execution. This document will help you execute scripts on Linux devices from the Hexnode UEM console.

Create and run Linux scripts

Here, we will show you how to create a simple Bash script that allows you to create local user accounts with standard privileges on Linux devices.

  1. On your Linux device, open the Terminal app.
  2. Choose a text editor (e.g., ‘nano’, ‘vim’, ‘emacs’, or ‘gedit’.) We will use ‘nano’ here, which is a terminal based text editor.
  3. Use the following command to open nano and create a new file:
  4. This command will open nano in the terminal with an empty buffer where you can start typing your script.

  5. Type the script into the terminal. For example, this script creates a local user account of standard privileges with the username and password provided.

    You can replace ‘SET_USERNAME’ and ‘SET_PASSWORD’ with actual values or pass them as arguments when executing the script.

  6. To save the script, press Ctrl + O which will ask for the confirmation of the file name. Press Enter.
  7. After saving, press Ctrl + X to exit the editor.
  8. Back in the terminal, make the file executable using the following command:

  9. Execute the script by running,

Execute custom scripts on Linux using Hexnode UEM

Hexnode UEM allows IT admins to execute custom scripts on Linux devices remotely using the ‘Execute Custom Script’ action.

Pre-requisites:

  • Works on Linux (Debian, Fedora & Ubuntu) devices.
  • Supported script file format: Bash(sh).
  • The file name of a script file should not contain values like / : ? \ * | “ [ ] @ ! % ^ and #.
  • Requires Hexnode Agent app to be installed on the device.
Warning:

It is recommended to validate the script execution manually on a system before executing in bulk.

To execute a custom script in Linux:

  1. On your Hexnode UEM portal, navigate to Manage > Devices.
  2. Select the Linux device to which the script is to be deployed.
  3. Click Actions > Execute Custom Script.
  4. Choose the script file source as either Upload file or from Hexnode repository.
    • If Upload file is selected, click on Choose file to select the corresponding script file from the device.
    • If Hexnode repository is selected, choose the script file that was previously uploaded in the Content tab in the Hexnode UEM portal.
  5. The Script name field will be auto-populated based on the name of the script file uploaded.
  6. The Binary path field gets auto-filled based on the type of added script. For Bash scripts, the binary path is /bin/bash.
  7. Note:

    If /bin/sh is the binary path shown for Bash scripts, you have to update the binary path to /bin/bash for compatibility and proper execution of the script.

  8. Enter the Arguments for the script file. Inputs are passed to the script in the form of arguments. Through Hexnode UEM, arguments can be remotely deployed to the device.
  9. Note:

    • When specifying arguments, you can use single quotes, and if you have multiple arguments, separate them with spaces.
    • Wildcards are also supported as arguments in Hexnode UEM, but their behavior depends on the shell (e.g., bash, zsh) used to run the script.

  10. Click on Execute to run the script.
  11. Navigate to the Action History sub-tab to view the execution status of the action.

The output details can be viewed by clicking the ‘Show Output’ button next to the corresponding action pushed.

What happens at the device end?

Once the Execute Custom Script action is successfully pushed to the device, the script will be automatically executed on the device end.

The uploaded script, in this case, creates a new local user account. IT admins can verify the account creation by navigating to Settings > System > Users on the Linux device, where the username will be visible under the Other Users tab.

Execute scripts on Linux devices to create local user accounts remotely.

  • Remote Actions