Skip to main content

Command

c.Command calls the API's Command group. Run a command on target hosts.

Methods​

MethodReturnsPermissionDescription
Exec(ctx, hostname, input)*Response[Collection[CommandResult]]command:executeRun a program with its arguments.
Shell(ctx, hostname, input)*Response[Collection[CommandResult]]command:shellRun a command line through /bin/sh -c.

Exec​

func (s *CommandService) Exec(
ctx context.Context,
hostname string,
input CommandExecInput,
) (*Response[Collection[CommandResult]], error)

Run a program with its arguments.

Runs the program with its arguments, each one word, with no shell between them: a ; or a | in an argument is that character. It answers with standard output and standard error apart, the exit code and the duration. A program that exits non-zero is a result, not an error. Only the admin role may run it, and the agent runs it only where agent.command.enabled is true.

It sends POST /api/node/{hostname}/command/exec, and needs the permission command:execute.

The examples of the method's definition:

Print a word, go run command.go exec:

resp, err := c.Command.Exec(ctx, target, client.CommandExecInput{
Args: []string{"hello"},
Command: "echo",
})

A program that exits non-zero, go run command.go exec-2:

resp, err := c.Command.Exec(ctx, target, client.CommandExecInput{Command: "false"})

In a directory, for at most 5 seconds, go run command.go exec-3:

resp, err := c.Command.Exec(ctx, target, client.CommandExecInput{
Command: "true",
Cwd: "/tmp",
Timeout: 5,
})

Shell​

func (s *CommandService) Shell(
ctx context.Context,
hostname string,
input CommandShellInput,
) (*Response[Collection[CommandResult]], error)

Run a command line through /bin/sh -c.

Runs the command line through /bin/sh -c, so pipes, redirects and variables work. It answers as exec does. Its permission is command:shell, apart from exec's command:execute: a token that may run a program may not open a shell. Only the admin role gets it.

It sends POST /api/node/{hostname}/command/shell, and needs the permission command:shell.

The examples of the method's definition:

A pipe, go run command.go shell:

resp, err := c.Command.Shell(ctx, target, client.CommandShellInput{Command: "echo hello | tr a-z A-Z"})

A command line that exits non-zero, go run command.go shell-2:

resp, err := c.Command.Shell(ctx, target, client.CommandShellInput{Command: "exit 3"})

Types​

The fields of each type are the properties of its schema in the API's spec.

CommandExecInput​

FieldTypeDescription
Args[]stringIts arguments, each one word, such as -la /tmp.
CommandstringThe program, a name on the agent's PATH or a path, such as ls.
CwdstringThe directory to run it in; the agent's own when empty.
TimeoutintSeconds to let it run before it is stopped, up to 300; 30 when 0.

CommandResult​

FieldTypeDescription
ChangedboolWhether the host changed: true, since a command can change anything.
DurationMSint64How long it ran, in milliseconds.
ErrorstringError message if the agent failed.
ExitCodeintIts exit code.
HostnamestringHostname of the agent that produced this result.
StatusRowStatusWhat happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran.
StderrstringWhat it wrote to standard error.
StdoutstringWhat it wrote to standard output.

CommandShellInput​

FieldTypeDescription
CommandstringThe command line, which /bin/sh -c reads, such as ls -la /tmp | grep log.
CwdstringThe directory to run it in; the agent's own when empty.
TimeoutintSeconds to let it run before it is stopped, up to 300; 30 when 0.

RowStatus​

What happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran.

A string, one of ok, failed, skipped, timeout.

Example​

examples/sdk/client/command.go runs each example of this page. With no argument, it lists them.