24

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,195
  • 5
  • 30
  • 55

2 Answers2

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

will redirect stderr to /dev/null.

Sven
  • 97,248
  • 13
  • 177
  • 225
-3

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
  • 416
  • 4
  • 8
  • 2
    I downvoted the answer because it does not solve the issue which was asked here: % ssh -p test -oLogLevel=QUIET localhost Bad port 'test' – cstamas Oct 15 '18 at 21:36
  • The "issue" in the original post is `-p test`. `test` is not a valid port so `ssh` cannot possibly succeed when called with `-p test`. It makes no sense to suppress a real error, instead of just fixing it (or deleting the call to ssh in the first place since it does nothing useful except printing the error). This answer is in general the best in answering "how to quiet ssh warnings" (as opposed to real errors). It is in fact how I got here, and it helped me the most. Down-voting because it doesn't solve the specific issue OP had, seems to be a bit harsh. – arielf Apr 20 '22 at 00:49
  • just add a `-p` and a port number - it still works, quietly `:-)` – todd_dsm Apr 21 '22 at 03:43