Skip to content

SSH script (ssh_script) ​

Runs one program that already exists on the target over SSH and reads its result. The result is in Probeward's own JSON format, or in the JSON or XML format of the incumbent's custom sensors, so a script written for either can be reused by choosing the format. The parsers are those of the script sensor: read its pages for the native format, the incumbent's format and where channel names and units come from.

Where script runs programs on the Probeward host, this sensor runs them on the monitored machine, as the SSH account of its credential. Nothing is installed on the Probeward host and PROBEWARD_SCRIPT_DIR is not used. Read the security model first.

Only administrators can attach or change this sensor.

What it looks like ​

The page of an ssh_script sensor in the light theme

The page of an SSH script sensor after a few polls, running a small test program on a lab server that reports the depth of a queue. In the dark theme: the sensor's page. The form for adding it, with its parameters filled in: light, dark.

Channels ​

KeyNameUnitDefault limits
run_msRun time (connecting, signing in and running the program)mswarning above 10000
one per channel the program reportsits name (its key if it gave none)its unitnone. Set limits on the sensor.

run_ms is reserved: a program that reports it fails the poll.

Parameters ​

ParameterDefaultMeaning
commandrequiredThe absolute path of the program on the target, such as /usr/local/bin/queue-depth. Letters, digits, ., _, -, + and / only, at most 256 characters, no . or .. segments. It is a path, not a command line.
argsnoneUp to 32 arguments of at most 1024 bytes, without control characters. {address} and {hostname} are replaced by the device's address and host name.
outputrequirednative_json, incumbent_json or incumbent_xml.
host_keynoneThe SHA-256 fingerprint of the server's host key as ssh-keygen -lf prints it: SHA256: and 43 characters. See the host key.
trust_on_first_usefalseWithout host_key: accept the key the server shows on the first poll. See the host key.
port22TCP port of the SSH server.

Suggested interval: 300 seconds. Credential: SSH (ssh_password or ssh_key), the same credential the other SSH sensors and sftp use. Either host_key or trust_on_first_use has to be set; a sensor with neither is refused when it is saved.

How the command is sent ​

The rule is a fixed command path and individually quoted arguments in one exec request. The sensor sends exactly this, and nothing else, to the server:

echo probeward-begin; /usr/local/bin/queue-depth 'one' 'two words' '; reboot' 'it'\''s'
  • The program is named by a path that holds no character a shell reads anything into, so it needs no quoting and cannot be anything but a path. A parameter that holds a space, ;, |, $, a backtick, a quote or a line break is refused when the sensor is saved and again before every run.
  • Every argument is wrapped in single quotes, with each single quote written as '\''. Inside single quotes a POSIX shell and fish read nothing but the quote, so ; reboot, $(id) and a backtick are plain characters of one argument. The program receives exactly the strings you wrote, which the live test checks against a real server.
  • No part of the command line comes from the monitored side, from the device's name or from anything the program printed.
  • The echo probeward-begin; in front is how output that the account's shell prints before the command (a banner from a profile) is told apart from the result: everything before the marker is dropped.

The server runs the line with the account's login shell, so the shell has to understand single quotes: sh, bash, dash, zsh and fish do. An account whose shell is csh, a restricted menu or a forced command cannot be used. Use an account of its own for this, with a normal shell, and give it no rights the program does not need.

The host key ​

The sensor does not send a credential to a server it has not identified. One of two things has to be set:

  • host_key (recommended). Take the fingerprint from the server by a way you trust, for instance on the machine itself with ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub, and put it in the parameter. The poll fails before any credential is sent when the server shows another key, and the message says so (SSH host key changed: ... but host_key pins ...). After a deliberate key change, set the new fingerprint.
  • trust_on_first_use. The first poll accepts whatever key the server shows and records it in the sensor's state; later polls insist on it, as the other SSH sensors do. This is weaker: anyone who can answer in the server's place during that first poll is trusted from then on, and receives the credential. So every poll of such a sensor ends its message with [warning: host key SHA256:... was trusted on first use; set host_key to pin it]. Copy the fingerprint from that message into host_key once you have checked it.

The two cannot be combined. A key is never accepted silently.

On the target ​

A program, a user that may run it, and a login shell. The program writes its result on standard output and exits with status 0; any other status is a collection failure, and the first line of what the program wrote to standard error or standard output is in the message. A status of 126 or 127 means the file is not executable or is not there. Output over 256 KiB fails the poll. The poll's timeout covers connecting, signing in and the run. When it passes, the connection is closed and the poll fails. Whether the program then ends is up to the server (OpenSSH usually hangs it up), and anything it started on its own stays on the target.

sh
#!/bin/sh
# /usr/local/bin/queue-depth: reports the number of jobs waiting in the queue.
n=$(ls /var/spool/jobs 2>/dev/null | wc -l)
printf '{"status":"ok","message":"%s jobs waiting","channels":[{"key":"waiting","name":"Jobs waiting","value":%s,"unit":"jobs"}]}\n' "$n" "$n"

Failures ​

MessageMeaning
a refused connection (kind unreachable), a timeout (kind timeout)The SSH server is not listening, a firewall drops the traffic, or the program ran past the timeout.
SSH host key changed: ... but host_key pins ... (kind protocol)The server shows another key than the pinned one. Nothing was sent.
... rejected the SSH credential for user ... (kind auth_failed)The user name, password or key is wrong.
/path exited with status 127: ... (kind protocol)The program is not there (126: it is not executable).
/path exited with status N and its output could not be read (...) (kind protocol)The program failed.
the output of /path could not be read: ... (kind protocol)The output is not in the chosen format.
/path reported an error: ... (kind protocol)The program said so itself with error.
missing credential, a credential that is not SSH (kind config)Set an SSH credential on the sensor, its device or a group above it.

Verification ​

Checked against scripted SSH servers (the three formats, the exact command line, a wrong pin that stops before anything runs, trust on first use and its warning, exit statuses, banners, hostile parameters), and live against an OpenSSH server on Debian 13 with a test account whose program prints a result in each format. The live run sent arguments that hold spaces, ;, $(...), a backtick, a quote, * and an empty string, and the program received each exactly once as one argument; nothing was executed. Wrong pins, trust on first use with a recorded key that differs, a failing program, a missing program and a wrong password each gave the failure above. Not seen: an account whose shell is not POSIX-like or fish, a Windows SSH server, a program that outlives the timeout, host keys other than ed25519.

Testing ​

The live tests in crates/probeward-sensors/tests/ssh_script_live.rs need PROBEWARD_TEST_SSH_SCRIPT_HOST, PROBEWARD_TEST_SSH_SCRIPT_USER, PROBEWARD_TEST_SSH_SCRIPT_PASSWORD, PROBEWARD_TEST_SSH_SCRIPT_COMMAND (the path of a test program that takes the format as its first argument and reports the number of its further arguments, see the comment at the top of the file) and PROBEWARD_TEST_SSH_SCRIPT_HOST_KEY, and skip cleanly without them.

AGPL-3.0-only