bash - 使 “Authenticity of host cannot be established”消息出现脚本错误

标签 bash shell ssh

我正在编写一个包装器脚本,该脚本将无密码的ssh设置到其他服务器,以供其他脚本使用。

问题在于内部脚本无法接收用户输入。例如,每当收到

The authenticity of host 'hostname.server (XXX.XXX.XX.XX)' can't be established.

RSA key fingerprint is RS:A-:FI:NG:ER:PR:IN:T-.

Are you sure you want to continue connecting (yes/no)?



并且用户输入了任何内容(“否”,"is"或“否”,然后按Enter键),内部脚本失败,因为它们不希望输入。我对内部脚本没有任何控制,也无法对其进行编辑。

我的包装器脚本实际上设置了ssh,并且还在其他脚本运行之前将ssh测试到服务器中。

我希望包装器脚本在需要用户输入的任何ssh响应上出错(如上述提到的那样)以及任何类型的ssh错误,因此该失败早在包装器脚本中而不是内部脚本中就可以知道。

有什么好的方法?

最佳答案

您要查找的SSH选项是BatchMode

从手册页:

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

设置连接超时可能也很有值(value),这样网络问题就不会导致您的ssh客户端永远挂掉:
 ConnectTimeout
         Specifies the timeout (in seconds) used when connecting to the
         SSH server, instead of using the default system TCP timeout.
         This value is used only when the target is down or really
         unreachable, not when it refuses the connection.

ConnectTimeout的默认值可能是0,这将导致ssh永远等待。

您可以将其放在命令行中:
ssh -o BatchMode=yes -o ConnectTimeout=15 me@host.example.com

或在类似~/.ssh/config的配置文件中:
host example host.example.com
    hostname host.example.com
    user me
    BatchMode yes
    ConnectTimeout 15

请注意,如果连接失败,SSH将在其退出值中返回一个状态,因此您可以使用如下的shell构造:
if ! ssh example bin/do_something; then
  echo "ERROR: bailing out of $0." >&2
  exit 1
fi

ssh命令还将产生标准错误,解释其失败原因。

关于bash - 使 “Authenticity of host cannot be established”消息出现脚本错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48424648/

相关文章:

regex - 合并两个 sed 命令

bash - 从文本文件中删除特殊字符(<200c> <200d>

shell - 通过Makefile通过ssh发出几个命令时的空行

linux - $* 和 $@ 有什么区别

windows - Beego(戈朗): Create new project via CLI in Windows

ssh - 是否可以在 USB 上运行 Vagrant box?

amazon-web-services - AWS - 断开连接 : No supported authentication methods available (server sent :publickey)

linux - 执行 if...then...fi 时出现 Cygwin 错误 - Bash 脚本

linux - 如何转义作为 shell 脚本中命令的关键字?

linux - 使用 Linux 命令合并和比较文件