SSH/Sftp log in fail recovery

I am connecting to X number of boxes and using sftp to grab files that need to backed up. the box i am doing it from should have keys set up, but in case something happens and it doesnt i need my script to recover from it and not sit there waiting for a password for 10 min. And i need to log which box failed so the problem can be fixed. Is there an easy way to test connect to each box and log when the test fails?