Shell Loop Interaction with SSH

If you have a bash script with a while loop that reads from stdin and uses ssh inside that loop, the ssh command will drain all remaining data from stdin1. This means that only the first line of data will be processed.

I encountered this issue yesterday2. This website explains why the behavior occurs and how to avoid it.

A flawed method to run commands on multiple systems entails a shell while loop over hostnames, and a Secure Shell SSH connection to each system. However, the default standard input handling of ssh drains the remaining hosts from the while loop

from Shell Loop Interaction with SSH.


  1. This is not only true for ssh but for any command in the loop that reads from stdin by default
  2. I won’t go into details here, since it is for a very specialized purpose. I will say that it involved jot, ssh, an aging Solaris based network appliance, and some new fangly XML/Web 2.0

Some ssh Tricks

I found this website with a bunch of ssh tricks. Some highlights:

Compare a Remote File with a Local File

ssh user@host cat /path/to/remotefile | diff /path/to/localfile -

Useful for checking if there are differences between local and remote files.

opendiff1 and bbdiff2 do not use stdin for their input, but you can work around that by copying the file to /tmp first:

scp user@host:/path/to/remotefile /tmp/remotefile && opendiff /path/to/localfile /tmp/remotefile

SSH Connection through host in the middle

ssh -t reachable_host ssh unreachable_host

Unreachable_host is unavailable from local network, but it’s available from reachable_host’s network. This command creates a connection to unreachable_host through “hidden” connection to reachable_host.

Using the -t option uses less overhead on the intermediate host. Same trick is used later in the article where you directly attach to a remote screen session:

ssh -t remote_host screen -r

Though I prefer using screen -DR. Read the man page for details.

The next one however didn’t do anything for me, I suspect there is a piece missing in the command somewhere:

Remove a line in a text File

sed -i 8d ~/.ssh/known_hosts

However there is a dedicated tool for this: use

ssh-keygen -R host

instead. I re-image some machines over and over again and then run into the ssh host key errors. This is very useful.


  1. Part of the Developer Tools installed with Xcode
  2. One of the tools installed by BBEdit