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.)
Nice trick! But the return code should be non-zero to signal an error: