0

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 [email protected])

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,

1 Answer 1

2

%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 [email protected] 2>&1)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.