Question

I want to store output of a ssh command as a string, so that I could do some scripting around it, like so:

ssh_command = %x(ssh -t -t user@ip.ip.ip.ip)

The problem is that 'ssh_command' does not actually seems to store any string. It's supposed to store this(for a non-valid IP):

ssh: connect to host ip.ip.ip.ip port 22: No route to host

It does output it in the irb but not as a string referenced by the 'ssh_command' variable.

Interestingly enough, the following works as expected:

uname_cmd = %x(uname -a)

In this case, when I print 'uname_cmd', I do get the string output back, as expected.

So my question is, is there a way to store the output of a ssh command as a regular string, like it works with uname?

Thanks,

Was it helpful?

Solution

%x(..) captures stdout, while ssh error messages are written to stderr.

You can redirect stderr to stdout so they're both captured using shell redirection syntax 2>&1:

ssh_command = %x(ssh -t -t user@ip.ip.ip.ip 2>&1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top