简体   繁体   中英

SSH: Don't prompt for password in shell script

I'm using ssh and have a passwordless ssh setup for my system. Using ssh in a script works fine, but if the user specifies a host that don't has a passwordless ssh setup the script prompts for a password and hangs.

How could I avoid the password prompt and return an error if passwordless ssh is not setup for the host?

ssh -o PasswordAuthentication=no user@hostname

有关更多详细信息,请参见ssh_config手册页。

Setting PasswordAuthentication didn't work for me but this did:

ssh -o BatchMode=yes user@hostname

From ssh_config(5)

BatchMode If set to ''yes'', passphrase/password querying will be disabled. This option is useful in scripts and other batch jobs where no user is present to supply the password. The argument must be ''yes'' or ''no''. The default is ''no''.

For good measure you could also throw in StrictHostKeyChecking so it will automatically accept keys and avoid the prompt.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM