Exit SSH if it is interactive

I am writing an automation that will ssh into hundreds of system and run a few commands. I ll be looping from ip X.X.X.10 to X.X.X.200

I have public key set up ready for "most" of them to run ssh non interactively. However some of the systems in these ip range do not have the public private key pair added. That makes my script interactive while doing an ssh to these box. I do not the the IPs of the which where the key pair has not been added

Is there any way to find before doing an ssh to a system if my public key is on that system. Conversely if the script has become interactive, is there a way to exit that particular ssh and continue to next one

Thanks in advance

If I understand you right, you want to avoid your ssh command to hang around waiting for you to enter a passwort, right?

If so, you can use

ssh -o BatchMode=yes hostname

This deactivates the prompt for a password. If you try to ssh into a host where your public key is not in the authorized_keys file, you get Permission denied.

You also might run into trouble if you try to ssh into a host you have never been before (the host is not in your clients known_hosts file). In that case you get a Host key verification failed error. To avoid that, you can use

ssh -o BatchMode=yes -o StrictHostKeyChecking=no hostname

Hope this helps.

I just did a little digging and found what i was looking for. Use
ssh -o 'PreferredAuthentications=publickey'