Command
c.Command calls the API's Command group. Run a command on target hosts.
Methods
| Method | Returns | Permission | Description |
|---|---|---|---|
Exec(ctx, hostname, input) | *Response[Collection[CommandResult]] | command:execute | Run a program with its arguments. |
Shell(ctx, hostname, input) | *Response[Collection[CommandResult]] | command:shell | Run 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
| Field | Type | Description |
|---|---|---|
Args | []string | Its arguments, each one word, such as -la /tmp. |
Command | string | The program, a name on the agent's PATH or a path, such as ls. |
Cwd | string | The directory to run it in; the agent's own when empty. |
Timeout | int | Seconds to let it run before it is stopped, up to 300; 30 when 0. |
CommandResult
| Field | Type | Description |
|---|---|---|
Changed | bool | Whether the host changed: true, since a command can change anything. |
DurationMS | int64 | How long it ran, in milliseconds. |
Error | string | Error message if the agent failed. |
ExitCode | int | Its exit code. |
Hostname | string | Hostname of the agent that produced this result. |
Status | 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. |
Stderr | string | What it wrote to standard error. |
Stdout | string | What it wrote to standard output. |
CommandShellInput
| Field | Type | Description |
|---|---|---|
Command | string | The command line, which /bin/sh -c reads, such as ls -la /tmp | grep log. |
Cwd | string | The directory to run it in; the agent's own when empty. |
Timeout | int | Seconds 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.