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.labfrom recipe 2.5, and only reads from it and writes to/tmp. - On that host, an ordinary account
vcfguidewith a password, used only to register the key. It needs nosudorights. - A login to the Orchestrator Client with administrator rights, and the action module
com.vcfguide.chapter5from recipe 5.1.
How to do it
Give Orchestrator a key pair
- Go to Library > Workflows, search with the tag
ssh, and run Generate key pair. - Choose the key type RSA and the size 4096, enter a passphrase, and click Run. Keep the passphrase: every key-based login needs it.
- Run the workflow that registers Orchestrator’s public key on a host (search the tag
sshfor public key). Enter the hostsyslog01.vmw.lab, the uservcfguide, and its password, and click Run. It copies the public key into~/.ssh/authorized_keysofvcfguideon the host.
Run dialog of Generate key pair: key type RSA, size 4096, passphrase field filled in (hidden).
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
- Search with the tags
sshandconfiguration, and run Add SSH Host. On the General Information tab, enter the host namesyslog01.vmw.laband leave the port at22. - 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. - Go to Administration > Inventory > SSH. Expand
syslog01.vmw.labto browse its file system asvcfguide.
Run a command without code
- Search with the tag
sshand run Run SSH command. Enter the host namesyslog01.vmw.laband the commanduptime. - On the Authentication tab, enter the user
vcfguide, leave key authentication selected, and enter the passphrase. Click Run. - 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 is0.
Run of Run SSH command against syslog01.vmw.lab with uptime, Variables tab with the output line and exitCode = 0.
Run commands from an action
- Create the action
runSshCommandincom.vcfguide.chapter5. Give it the inputshostName,userName,keyPath, andcommandof typestring, andpassphraseof typeSecureString, and the Return typeProperties:
// 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;- Run the action with
syslog01.vmw.lab,vcfguide, the key path from step 5, the passphrase, and the commanddf -h /var/log. The result’soutputis thedftable andexitCodeis0. - Run it again with the command
ls /nonexistent. The action fails, and the error names the command, the exit code2, and the message fromls.
Failed run of action com.vcfguide.chapter5/runSshCommand with ls /nonexistent: the error message with exit code 2 and the No such file or directory text, and the Logs tab with the Running on line.
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
- Create the action
copyFilesWithHostwith the samehostName,userName,keyPath, andpassphraseinputs, and the Return typestring:
// 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;- Run it. The result is the first line of the host’s
/etc/os-release, such asNAME="Ubuntu". On the host,cat /tmp/vcfguide-hello.txtshows 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.
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.
js-io-rights.conf directly on the appliance's file system, where the server also ran./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).
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
- 2.8 Setting system properties that matter, for
js-io-rights.confand the server’s file system. - 3.4 Handling errors and exceptions, for what happens after the action throws.
- 4.7 Working with configuration elements in code, for keeping the passphrase out of workflows.
- Broadcom TechDocs (VCF 9.0): Configuring the SSH Plug-In and Running the SSH Plug-In Sample Workflows. VCF 9.1: Generate a Key Pair in VCF Operations Orchestrator.
- Broadcom TechDocs (Aria Automation 8.17 and 8.18): Add an SSH host and Run an SSH command.
- Broadcom KB 454438 (9.x, FIPS mode), 379994 and 382628 (key logins to RHEL 9).
