Skip to content

SSH commands and file copy

Not everything has an API. Appliances, Linux servers, network devices, and old tools are often easiest to reach with a shell command. The SSH plug-in lets a workflow log in over SSH, run a command, read what it printed and its exit code, and copy files to and from the host.

In this recipe you give Orchestrator its own key pair so that it can log in without a password, register the public key on the lab’s Linux host syslog01.vmw.lab, run a command with the built-in workflow, and then write an action that runs commands and fails properly when they fail. Last, you copy a file to the host and another one back.

Getting ready

  • A Linux host Orchestrator can reach on TCP 22. This recipe uses the lab’s syslog server syslog01.vmw.lab from recipe 2.5, and only reads from it and writes to /tmp.
  • On that host, an ordinary account vcfguide with a password, used only to register the key. It needs no sudo rights.
  • A login to the Orchestrator Client with administrator rights, and the action module com.vcfguide.chapter5 from recipe 5.1.

How to do it

Give Orchestrator a key pair

  1. Go to Library > Workflows, search with the tag ssh, and run Generate key pair.
  2. Choose the key type RSA and the size 4096, enter a passphrase, and click Run. Keep the passphrase: every key-based login needs it.
  3. Run the workflow that registers Orchestrator’s public key on a host (search the tag ssh for public key). Enter the host syslog01.vmw.lab, the user vcfguide, and its password, and click Run. It copies the public key into ~/.ssh/authorized_keys of vcfguide on the host.
Figure 5.7. Generating Orchestrator's key pair.

On 9.1.1 the SSH folder of the library has Generate key pair and Register VCF Operations Orchestrator server public key on host (the old Register vCO public key on host), with Change key pair passphrase beside them. Generate key pair takes a key type, rsa, dsa or ecdsa (rsa if you leave it empty), a size (2048 for RSA and DSA, 521 for ECDSA if empty), a passphrase and a comment, and stores the key pair at /var/lib/vco/app-server/conf/vco_key, the path the action com.vmware.constants/getDefaultSSHKeyPairPath returns. The register workflow logs in with the password you give it, copies vco_key.pub to /tmp on the host, and runs mkdir -p .ssh and appends the key to ~/.ssh/authorized_keys, so a missing ~/.ssh is created. It doesn’t set permissions on it, so check that ~/.ssh is 700 and authorized_keys 600 if the key is refused.

Register the host in the inventory

  1. Search with the tags ssh and configuration, and run Add SSH Host. On the General Information tab, enter the host name syslog01.vmw.lab and leave the port at 22.
  2. On the Authentication tab, enter the user name vcfguide, choose key authentication instead of a password, and enter the path to the private key and its passphrase. Click Run.
  3. Go to Administration > Inventory > SSH. Expand syslog01.vmw.lab to browse its file system as vcfguide.
Needs a lab check. Confirm the tab and field labels of Add SSH Host on 9.1.1 (the 8.17 workflow has General Information and Authentication tabs and a root-folder field), the default private key path it fills in, and whether the host’s SSH host key is checked or stored anywhere.

Run a command without code

  1. Search with the tag ssh and run Run SSH command. Enter the host name syslog01.vmw.lab and the command uptime.
  2. On the Authentication tab, enter the user vcfguide, leave key authentication selected, and enter the passphrase. Click Run.
  3. On the run’s Variables tab, the output holds a line such as up 12 days, 3 users, load average: 0.02, and the exit code is 0.
Figure 5.8. A command run on a Linux host, with its output and exit code.

Run commands from an action

  1. Create the action runSshCommand in com.vcfguide.chapter5. Give it the inputs hostName, userName, keyPath, and command of type string, and passphrase of type SecureString, and the Return type Properties:
com.vcfguide.chapter5/runSshCommand
// Runs one command on a host as userName, with Orchestrator's private key.
// Returns output, error, and exitCode; throws if the command fails.
var session = new SSHSession(hostName, userName);
var result = new Properties();
try {
session.connectWithIdentity(keyPath, passphrase);
System.log("Running on " + hostName + ": " + command);
session.executeCommand(command, true); // true: wait until it finishes
result.put("output", session.output);
result.put("error", session.error);
result.put("exitCode", session.exitCode);
} finally {
session.disconnect();
}
if (result.get("exitCode") !== 0) {
throw "runSshCommand: '" + command + "' on " + hostName + " exited with "
+ result.get("exitCode") + ": " + result.get("error");
}
return result;
  1. Run the action with syslog01.vmw.lab, vcfguide, the key path from step 5, the passphrase, and the command df -h /var/log. The result’s output is the df table and exitCode is 0.
  2. Run it again with the command ls /nonexistent. The action fails, and the error names the command, the exit code 2, and the message from ls.
Figure 5.9. The action reporting a failed command.
Needs a lab check. Check on 9.1.1 that SSHSession.exitCode arrives in JavaScript as a number (so that !== 0 works), that output and error hold the whole of standard output and standard error, and that a SecureString input can be passed straight to connectWithIdentity.

Copy files both ways

  1. Create the action copyFilesWithHost with the same hostName, userName, keyPath, and passphrase inputs, and the Return type string:
com.vcfguide.chapter5/copyFilesWithHost
// Copies a small file to the host, then copies /etc/os-release back
// and returns its first line. Local files must be under /var/run/vco.
var localOut = "/var/run/vco/vcfguide-hello.txt";
var localIn = "/var/run/vco/vcfguide-os-release.txt";
var writer = new FileWriter(localOut);
writer.open();
writer.writeLine("Hello from Orchestrator at " + new Date());
writer.close();
var session = new SSHSession(hostName, userName);
try {
session.connectWithIdentity(keyPath, passphrase);
if (session.putFile(localOut, "/tmp/vcfguide-hello.txt") !== 0) {
throw "copyFilesWithHost: copy to " + hostName + " failed";
}
if (session.getFile("/etc/os-release", localIn) !== 0) {
throw "copyFilesWithHost: copy from " + hostName + " failed";
}
} finally {
session.disconnect();
}
var reader = new FileReader(localIn);
reader.open();
var firstLine = reader.readLine();
reader.close();
System.log("Copied both ways with " + hostName + ": " + firstLine);
return firstLine;
  1. Run it. The result is the first line of the host’s /etc/os-release, such as NAME="Ubuntu". On the host, cat /tmp/vcfguide-hello.txt shows the line the action wrote.

Checked on the embedded 9.1.1 lab: a script can write to /var/run/vco with FileWriter without changes to js-io-rights.conf. The API reference gives getFile(remoteFile, localFile) and putFile(localFile, remoteFile), both returning a number, and Broadcom’s own register workflow treats anything but 0 from putFile as a failure, as step 13 does.

Needs a lab check. Run step 13 against an SSH host on 9.1.1 and confirm that getFile can write into /var/run/vco and that both calls return 0.

How it works

The SSH plug-in is an SSH client inside the Orchestrator server, built on the Java library JSch. Each SSHSession is one SSH connection: connectWithIdentity logs in with a private key, connectWithPassword with a password, and executeCommand opens a channel on that connection, runs the command in the user’s shell, and collects its standard output, standard error, and exit code. With true as the second argument, the script waits until the command ends. One session can run several commands, one after another, which is cheaper than logging in for each.

The exit code is the only reliable signal. The plug-in doesn’t decide whether a command worked. A command that fails still gives you an output, an error text, and an exit code, and the run carries on happily unless the script checks. The action turns a non-zero exit code into an exception, so the workflow’s error handling (recipe 3.4) takes over.

Orchestrator’s key pair. Generate key pair creates one key pair for the whole server and stores the private key on the server, protected by the passphrase. The public key goes onto each host, so a workflow can log in without a password stored anywhere in Orchestrator. Generating a new pair replaces the old one, and every host that had the old public key stops accepting logins until you register the new one. Any workflow author on the server can use the key, so the account it logs in to should have only the rights the workflows need, as vcfguide does here.

Files go through the server. putFile and getFile copy between the host and the Orchestrator server’s own file system, using SFTP over the session. The server runs in a container where scripts may only write to a few places. /var/run/vco is writable by default, which is why both files live there. Other paths need a rule in js-io-rights.conf (recipe 2.8). On a cluster, each node has its own /var/run/vco, so copy a file in and use it in the same script, not in a later workflow element that may run on another node.

Back in vRO 5.5–7Run the SSH workflows from the Java client, and open up file access for SCP by editing js-io-rights.conf directly on the appliance's file system, where the server also ran.
In Orchestrator 9.xKeep the key that Generate key pair creates on the server, copy files through /var/run/vco inside the server's container, and change file access in /data/vco/usr/lib/vco/app-server/conf/js-io-rights.conf only when you must.

There's more

Commands that need sudo. executeCommand has no terminal, so sudo can’t ask for a password. If a workflow must run something as root, give the account a narrow sudoers rule with NOPASSWD for exactly the commands it needs, such as /usr/bin/systemctl restart rsyslog, and run sudo -n … so that a missing rule fails at once instead of waiting.

Quoting. The command runs in the remote user’s shell, so anything you build from workflow inputs is shell code. Never add free text from a form to a command. Validate inputs against a strict pattern first, as recipe 3.6 does for form fields, or pass values in a file you copy over.

Long-running commands. A command that takes minutes holds the session and the workflow. Start it in the background on the host, such as nohup /opt/scripts/backup.sh > /tmp/backup.log 2>&1 &, and poll for the result with a short command and a waiting timer (recipe 3.7).

Hosts that reject the key. Older SSH plug-ins signed key logins with SHA-1, which RHEL 9 and similar systems refuse by default; the login failed with Too many authentication failures. Broadcom fixed this in SSH plug-in 8.0.1. If key logins fail on 9.x, check the network first: with FIPS mode on, a blocked port can show up as Your private key is encrypted with a non-compliant cipher instead of a time-out (KB 454438).

The 9.1.1 appliance ships the SSH plug-in as version 9.1.1.0, like every other built-in plug-in (GET /vco/api/plugins).

Needs a lab check. Check whether key login to a RHEL 9 host with its default crypto policy works with an RSA key from step 2.

Password logins. For a quick test, connectWithPassword(password) works, with the password read from a configuration element (recipe 4.7). Keys are better: there’s no password to rotate in Orchestrator, and you can remove one host’s access by deleting one line in its authorized_keys.

Your own key. The Generate key pair page in Broadcom’s documentation explains how to use a key you created elsewhere: it must be in PEM format, which ssh-keygen -m pem creates.

See also