Linux SSH:不要在 shell 脚本中提示输入密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10180128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:49:15  来源:igfitidea点击:

SSH: Don't prompt for password in shell script

linuxbashsshrsa-key-fingerprint

提问by multiholle

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.

我正在使用 ssh 并为我的系统设置了无密码 ssh。在脚本中使用 ssh 工作正常,但如果用户指定的主机没有无密码 ssh 设置,脚本会提示输入密码并挂起。

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

如果没有为主机设置无密码 ssh,我如何避免密码提示并返回错误?

采纳答案by nosid

ssh -o PasswordAuthentication=no user@hostname

See man page of ssh_configfor more details.

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

回答by Mike

Setting PasswordAuthenticationdidn't work for me but this did:

设置PasswordAuthentication对我不起作用,但这样做了:

ssh -o BatchMode=yes user@hostname

From ssh_config(5)

来自 ssh_config(5)

BatchModeIf 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''.

BatchMode如果设置为“yes”,密码短语/密码查询将被禁用。此选项在没有用户提供密码的脚本和其他批处理作业中很有用。参数必须是“是”或“否”。默认值为“否”。

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

为了更好地衡量,您还可以加入StrictHostKeyChecking以便它会自动接受密钥并避免提示。

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