This question already has an answer here:

I am trying to achieve a scenario where I need to SSH to ABC server from XYZ server. Public keys will set up already to smooth password less login. Now i just want to exit from script if found that SSH command prompts for password. It means public key are not set up.

ssh -o ConnectTimeout=10 user@ABC <<HERE
----- doing some stuff here----



Script resides on XYZ. I am running it from XYZ and trying to SSH ABC. If it prompts for password then exit from script by printing a message.

marked as duplicate by Jeff Schaller, RalfFriedl, Isaac, thrig, elbarna Oct 11 '18 at 15:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Add PreferredAuthentications=publickey option to your connection string:

ssh -o PreferredAuthentications=publickey -o ConnectTimeout=10 user@ABC

The exit code will 255 and you will get this information:

user@ABC: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

  • I tried to use same but it did not work for me. I am still getting prompt to provide the password. I would like to exit rather wait whenever my ssh command prompt for password. This is what I need to achieve. – Vivek Pandey Oct 15 '18 at 10:45
  • @VivekPandey it works for me. Which version of SSH do you have installed? – mrc02_kr Oct 15 '18 at 11:10
  • its' OpenSSH_6.6.1p1. I used it like- "ssh -o PreferredAuthentications=publickey -o ConnectTimeout=10 user@ABC << HERE ------ do some stuff here on ABC from XYZ-------- HERE. But it still waits on password prompts, do not exit – Vivek Pandey Oct 15 '18 at 11:13
  • I have version 7.4 and 7.8 and it's working. Unfortunately I don't have access to machine with OpenSSH6.6 – mrc02_kr Oct 15 '18 at 11:20
  • Anyway to achieve this for OpenSSH6.6 machine – Vivek Pandey Oct 15 '18 at 11:26


ssh -o BatchMode=yes

according to man 5 ssh_config


       If set to “yes”, passphrase/password querying will be disabled.
         In addition, the ServerAliveInterval option will be set to 300
         seconds by default.  This option is useful in scripts and other
         batch jobs where no user is present to supply the password, and
         where it is desirable to detect a broken network swiftly.  The
         argument must be “yes” or “no”.  The default is “no”.

Add PasswordAuthentication=no to your ssh options

$ ssh -o PasswordAuthentication=no host
user@host: : Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
$ echo $?
  • I tried but it did not work. It may be the duplicate question but answers provided for previous questions did not work here. I am trying to ssh to another server and execute some commands on that server from source server. SSH keys are set up but if these are not there than script will prompt for password. I would like to quit if it prompts for password. – Vivek Pandey Oct 15 '18 at 10:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.