Red Green Repeat Adventures of a Spec Driven Junkie

TIL ssh command output

This week, I had to get some data from another Linux server in another process on that server.

I was thinking about how to connect to the remote server’s process to capture its output to use in a script.

I wanted to have a script automate as much of the process as possible. So logging into the other server to get data to finish the script did not make sense to me.

At the end, I learned that ssh can execute commands and put the output to the current system.

This is the format:

$ ssh server "server command"

Example

A quick demonstration to use this command, grabbing a list of processes on another Linux system.

$ ssh server "ps ae -o comm --no-headers | sort | uniq"
bash
getty
ps
sort
uniq

Using in a Script

Now, to get this in a bash script is pretty:

#!/usr/bin/env bash

processes=($(ssh server "ps ae -o comm --no-headers | sort | uniq"))

echo 'Remote server processes:' ${processes[@]}

Conclusion

By taking advantage of ssh’s command output, ssh can be used as another tool in automating scripts when working with remote systems.