Sending Commands to Devices

Single Device Commands

Commands can be sent to one device at a time from the device detail page, as shown in the GIF below:

Sending a single command

Click the Send Command button, select the command type and fill in the required inputs. The command is sent to the device and its status is shown in the Recent Commands section of the device detail page.

To send the same command to many devices at once, refer to Mass Commands.

Default Commands

By default, there are three options in the Send Command dropdown:

  1. Reboot

  2. Change Password

  3. Custom Command

While the first two options are self-explanatory, the custom command option allows you to execute any command on the device as shown in the example below.

Executing commands on device example

Important

In order for this feature to work, a device needs to have at least one valid Access Credential (see How to configure push updates).

The Send Command button will be hidden until the device has at least one Access Credential.

If you need to allow your users to quickly send specific commands that are used often in your network regardless of your users' knowledge of Linux shell commands, you can add new commands by following instructions in the Defining New Options in the Commands Menu section below.

Note

You can also use the REST API to execute commands on a device.

Mass Commands

Mass commands run the same command on many devices at once, for example to reboot all the devices of an organization, as shown in the GIF below:

Sending a mass command

Sending a Mass Command

  1. Open Network Operations > Run Mass command.

  2. Choose the command, a label to recognize it later, and the targets: organization, device group and location. Combining targets narrows the selection.

  3. Review the matched devices, uncheck any you want to leave out, and click Execute.

Superusers can leave every target empty to run the command on all the devices, as shown in the GIF below:

Running a mass command on all devices as a superuser

Other users must choose at least one target and can only use the commands enabled for their organization (see OPENWISP_CONTROLLER_ORGANIZATION_ENABLED_COMMANDS).

From the Device List

A mass command can also be started from the device list, as shown in the GIF below:

Sending a mass command to selected devices

Select the devices, choose Execute mass command from the actions dropdown and click Go. Then choose the command and review the devices as described above. The selected devices must belong to the same organization.

Following the Results

After executing, the mass command page shows the status and output of each device, updated in real time, as shown in the GIF below:

Following the results of a mass command
  • Devices which cannot run the command, for example because they have no access credentials, are marked as skipped together with the reason.

  • The output column shows only the last line of the output, limited to 100 characters.

  • If any device fails or is skipped, the mass command is marked as failed.

  • Past mass commands are listed in Network Operations > Mass commands.

  • Commands sent by a mass command also appear in the Recent Commands of each device, with a link back to the mass command.

REST API

Mass commands can also be sent and followed through the Batch Command API.

Defining New Options in the Commands Menu

Note

If you're developing an OpenWISP extension, use the functions described in Registering / Unregistering Commands in the developer documentation instead of the setting described below.

Let's explore to define new custom commands to help users perform additional management actions without having to be Linux/Unix experts.

We can do so by using the OPENWISP_CONTROLLER_USER_COMMANDS Django setting.

The following example defines a simple command that can ping an input destination_address through a network interface, interface_name.

# In yourproject/settings.py


def ping_command_callable(destination_address, interface_name=None):
    command = f"ping -c 4 {destination_address}"
    if interface_name:
        command += f" -I {interface_name}"
    return command


OPENWISP_CONTROLLER_USER_COMMANDS = [
    (
        "ping",
        {
            "label": "Ping",
            "schema": {
                "title": "Ping",
                "type": "object",
                "required": ["destination_address"],
                "properties": {
                    "destination_address": {
                        "type": "string",
                        "title": "Destination Address",
                    },
                    "interface_name": {
                        "type": "string",
                        "title": "Interface Name",
                    },
                },
                "message": "Destination Address cannot be empty",
                "additionalProperties": False,
            },
            "callable": ping_command_callable,
        },
    )
]

The above code will add the Ping command in the user interface as show in the GIF below:

Adding a *ping* command

The OPENWISP_CONTROLLER_USER_COMMANDS setting takes a list of tuple each containing two elements. The first element of the tuple should contain an identifier for the command and the second element should contain a dict defining configuration of the command.

Command Configuration

The dict defining configuration for command should contain following keys:

1. label

A str defining label for the command used internally by Django.

2. schema

A dict defining JSONSchema for inputs of command. You can specify the inputs for your command, add rules for performing validation and make inputs required or optional.

Here is a detailed explanation of the schema used in above example:

{
    # Name of the command displayed in *Send Command* widget
    "title": "Ping",
    # Use type *object* if the command needs to accept inputs
    # Use type *null* if the command does not accepts any input
    "type": "object",
    # Specify list of inputs that are required
    "required": ["destination_address"],
    # Define the inputs for the commands along with their properties
    "properties": {
        "destination_address": {
            # type of the input value
            "type": "string",
            # label used for displaying this input field
            "title": "Destination Address",
        },
        "interface_name": {
            "type": "string",
            "title": "Interface Name",
        },
    },
    # Error message to be shown if validation fails
    "message": "Destination Address cannot be empty",
    # Whether specifying addtionaly inputs is allowed from the input form
    "additionalProperties": False,
}

This example uses only handful of properties available in JSONSchema. You can experiment with other properties of JSONSchema for schema of your command.

3. callable

A callable or str defining dotted path to a callable. It should return the command (str) to be executed on the device. Inputs of the command are passed as arguments to this callable.

The example above includes a callable(ping_command_callable) for ping command.

How to register or unregister commands

Refer to Registering / Unregistering Commands in the developer documentation.