Don Marti

Mon 10 Oct 2011 09:46:37 PM PDT

SSH scripts: prompt for passphrase if needed

If you have a script that uses ssh, here's something to put at the beginning of the script to make sure the necessary passphrase has already been entered, and the remote host is reachable, before starting a time-consuming operation such as an rsync or offlineimap:

ssh-add -L > /dev/null || ssh-add
ssh $REMOTE_HOST true || exit 0

(If the ssh agent has no identities, prompt for a passphrase. Then exit if the remote system is not reachable.)

comment 1

Nice trick! But the return code should be non-zero to signal an error:

ssh $REMOTE_HOST true || exit 1
Comment by Anonymous Wed 12 Oct 2011 11:10:59 PM PDT