3

I have written a script that is expected to get a bunch of host names from some place and run a command on each of those hosts:

ssh "$host" some command

I have the necessary configuration to make ssh connections without the need for a password. However, sometimes we encounter a host that does need a password and the script hangs.

Is there a way to make ssh time out while waiting for a password? I don't want to use the timeout command.

2

You are looking for LoginGraceTime in your sshd_config.

From sshd_config(5):

LoginGraceTime
             The server disconnects after this time if the user has not successfully logged in.  If the value is 0, there is no time
             limit.  The default is 120 seconds.
5

Either of these options might work on the client side to prevent asking the password at all, which seems it should work in your case. (I assume you can find the failed the hosts from some log and rerun them interactively.) From ssh_config(5):

BatchMode
    If set to yes, passphrase/password querying will be disabled. ...

PasswordAuthentication
    Specifies whether to use password authentication.
    The argument to this keyword must be yes (the default) or no.

So,

ssh -oBatchMode=yes someuser@somehost ...

or

ssh -oPasswordAuthentication=no someuser@somehost ...
  • Nice answer. I wanted ssh to prompt for password but not be waiting forever if user doesn't enter the password. man0v's answer shows the way. – codeforester Feb 28 '18 at 7:07
  • @codeforester, yep. I tried to see if it can be done client-side, but I didn't seem to find anything with a timeout on the authentication part. – ilkkachu Feb 28 '18 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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