23
#!/bin/bash

ssh -t $SSH "
    some
    commands
"

Where is the 'connection to xx.xxx.xx.xxx closed' message coming from? I can't stop it even with result=ssh ...

11

That is coming from SSH. You see it because you gave the -t switch, which forces SSH to allocate a pseudo-terminal for the connection. Traditionally, SSH displays that message to make it clear that you are no longer interacting with the shell on the remote host, which is normally only a question when SSH has a pseudo-terminal allocated.

40

if you add -o LogLevel=QUIET to the SSH command line, that message should disappear:

ssh -o LogLevel=QUIET -t $SSH "
      some 
      commands
"

You can also add it to the ~/.ssh/config file as a line saying LogLevel QUIET

4
  • 1
    This should be the accpeted answer to me – Nam G VU Jul 22 '18 at 11:29
  • Perhaps this answer should be accepted! – indianwebdevil Oct 9 '18 at 5:26
  • Making SSH quiet also removes the useful host key fingerprint. I'd like to remove the "connection to XXXX closed" part (or remove XXXX from that message) – martin Jun 8 '20 at 5:58
  • Or use ssh -q as already mentioned in other comments. – famzah Jan 13 at 16:24
6

As Fran mentioned, this is coming about because of the -t switch. You can hide the message by appending:

 2> /dev/null

Your code would look like this:

#!/bin/bash

ssh -t $SSH "
    some
    commands
" 2> /dev/null

This redirects STDERR to /dev/null. Keep in mind all error messages that may be raised will also be redirected to /dev/null and so will be hidden from view.

2
  • 1
    There is problem here, if i want to log the output, this will not help. But other than that its good. – indianwebdevil Oct 9 '18 at 5:27
  • @indianwebdevil actually this is not going to hide the output of the script (nor the stderr from the remote commands you run), just your local stderr stream (basically other ssh warnings/errors you may get). – Diego Roccia Sep 13 '20 at 8:43
2

I know this is a pretty old question but it pops up in searches.

A better answer is here: How to hide or edit a message on closure of a ssh ,when ssh was handled using interact in expect script.

Basically, use the ssh -q option - it worked perfect for me.

-1

Could not comment on Fran's answer which is the correct one.

Just wanted to add that your code fragment looks like a script, which means you are likely calling remote stuff which is not depending on an interactive screen (unlike for example mc or top which make little sense without a keyboard and a screen - you will therefore not use these programs in scripts).

So you are better off explicitly without a terminal. That means replace the -t flag with the -T flag. Longer syntax which I prefer in scripts to make them more self evident and readable would be: -o 'RequestTTY no' vs. -o 'RequestTTY yes'.

And while at that, forget the -o LogLevel=QUIET solution suggested by Leo - it hides error messages too. If your script faces some unfavourable circumstances, you will not understand what went wrong. In scripts you probably want to use -o LogLevel=ERROR - this suppresses the remote machine's banner if it has one, but lets error messages through.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.