25

I'm half way through writing a nagios script and I've hit an annoyance with SSH.

According to the man page:

-q       Quiet mode.  Causes all warning and diagnostic messages to be
         suppressed.

Yet if I enable the quiet flag and then pass an invalid port, I still get an error:

$ ssh user@localhost -q -p test
Bad port 'test'

This is a problem, because that will make that message the first line out and that's what is grabbed by Nagios. I need to output something like "Warning|SSH error" after picking up on a != 0 exit code from ssh, but the first line I can output on is going to be line 2.

How can I make SSH TRULY quiet?

Note: I wasn't sure whether to post this question on serverfault, on superuser or on stackoverflow. I went with serverfault as the user base are probably most experienced with cli SSH and cli scripting workarounds.

SimonJGreen
  • 3,285

3 Answers3

35
ssh user@localhost -q -p test 2> /dev/null 

will redirect stderr to /dev/null.

Sven
  • 100,763
1

I'd like to point out that the accepted answer will also redirect stderr of the process executed on the remote side as well, which is something I would generally like to keep:

$ ssh server -p 4444 "cat foo" 2> /dev/null # an error from remote sshd (bad port)
$ ssh server -p test "cat foo" 2> /dev/null # an error from local ssh (illegal port)
$ ssh server "cat foo" 2> /dev/null # an error from remote cat (no such file)

Using -q will quiet the errors from sshd, as will using -E /dev/null, which is the problem the original question was asking about:

$ ssh server -q -p 4444 "cat foo"
$ ssh server -q -p test "cat foo"
Bad port 'test'
$ ssh server -q "cat foo"
cat: foo: No such file or directory

There is probably a way to use additional file descriptors to keep remote stdout and stderr separate and analyze them separately on the local machine if needed, but for the casual grepper, redirecting stderr to stdout for the remote task should work fine in addition to the accepted answer's binning the stderr output:

$ ssh server -p 4444 "cat foo 2>&1" 2> /dev/null
$ ssh server -p test "cat foo 2>&1" 2> /dev/null
$ ssh server "cat foo 2>&1" 2> /dev/null
cat: foo: No such file or directory
Andrej
  • 51
0

Or, the easiest method store LogLevel QUIET in the config file:

$ cat ~/.ssh/config 
Host *
  IdentityFile ~/.ssh/id_rsa
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null
  LogLevel QUIET
  ...

Ends up getting the same result as above with lots less typing.

Cheers

todd_dsm
  • 545