我正在编写一个脚本来遍历一个已知的主机名列表,以便find一个可以使用我的SSH密钥进行身份validation的正在运行的SSH服务器。
密钥已经加载了ssh-agent和ssh-add ,所以如果我连接到一个知道我的密钥的正在工作的远程主机,没有提示密码,并且我没有任何交互成功连接。
事情是,当远程主机不知道我的密钥, ssh提示我的密码短语(它没有机会成功,因为,如果可能的话,它已经login了我感谢ssh-agent )。 我想阻止这种行为,并在ssh-agent身份validation失败时使ssh中止。
我目前使用下面的命令来绕过大部分的交互,但我不能阻止我刚才描述的那个:
$ ssh -i ~/.ssh/id_rsa \ -o UserKnownHostsFile=/dev/null \ -o KbdInteractiveAuthentication=no \ -o StrictHostKeyChecking=no \ -o PreferredAuthentications=publickey \ -o ConnectTimeout=1 \ $host -n "whoami"
感谢您的回答。
man ssh_config:
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''.
虽然BatchMode的答案当然是正确的,我想也提到,你也可以这样做:-o PasswordAuthentication = no; 同一个男人页面。