“ssh”有效,但“sshpass”无效——这怎么可能?

And*_*eyS 3 ssh rhel openssh sshpass

我为 SSH 连接配置了密钥对。它可以工作,但当然需要密码。

ssh user1@10.7.175.143
Run Code Online (Sandbox Code Playgroud)

所以现在我尝试使用我安装的 sshpass 登录。我尝试过使用-p财产,但也尝试过使用-f财产,但没有任何效果 - 它只是挂起。

verbose 在客户端提供这些信息

sshpass -v -p "pass" ssh user1@10.7.175.143
SSHPASS searching for password prompt using match "assword"
SSHPASS read:
SSHPASS read: Enter passphrase for key '/home/user1/.ssh/id_rsa':
Run Code Online (Sandbox Code Playgroud)

在服务器端我可以在日志中看到这些信息:

Accepted key RSA SHA256:V/V29pA2Ps5k/lBgz2R5XFP6vaaaOUN5hj0hca+j8TI found at __PROGRAMDATA__/ssh/administrators_authoriz
ed_keys:1
debug3: mm_answer_keyallowed: publickey authentication test: RSA key is allowed
debug3: mm_request_send: entering, type 23
debug3: send packet: type 60 [preauth]
debug2: userauth_pubkey: authenticated 0 pkalg rsa-sha2-256 [preauth]
debug3: user_specific_delay: user specific delay 0.000ms [preauth]
debug3: ensure_minimum_time_since: elapsed 19.018ms, delaying 1.849ms (requested 5.217ms) [preauth]
Postponed publickey for user1 from 10.7.141.243 port 44750 ssh2 [preauth]
Run Code Online (Sandbox Code Playgroud)

非常感谢您的热情帮助!

Kam*_*ski 7

ssh使用终端 ( /dev/tty)(而不是其标准输入)提示并读取密码(或密码短语)。这样您就可以通过管道传输/重定向数据,ssh并且仍然能够在被要求时提供密码。但要不通过终端提供密码,需要向 提供一个“假”终端ssh。这就是sshpass作用。

\n

当您在专用的模拟终端中运行sshpass \xe2\x80\xa6 ssh \xe2\x80\xa6时。这意味着不会直接从您的终端读取。并且不会直接打印到您的终端。最终将充当中继,因此就像使用您的终端一样。但在此之前,拦截打印内容;它还会注入您在之后指定的字符串,然后“看到”该字符串来自正在使用的终端(不是您的终端)。这种方式可能会欺骗您输入密码,当它是sshpasssshsshsshpasssshsshpasssshpasssshsshpassssh-psshsshsshsshpass“输入”的人。

\n

默认情况下,sshpass等待assword:(或assword1?)作为密码提示的一部分出现。例如,如果您没有使用密钥并且没有使用sshpass,ssh将打印:

\n
user1@10.7.175.143\'s password:\n
Run Code Online (Sandbox Code Playgroud)\n

它会等待您输入密码。如果您曾经sshpass提供密码,那么sshpass将拦截此消息并为您“输入”密码。通过等待正确的提示sshpass知道何时ssh需要密码,只有这样它才会传递您的密码。

\n

在你的情况下,提示有所不同。ssh没有要求输入密码,它使用不同的提示要求输入密码。来自的提示ssh完全正确Enter passphrase for key \'/home/user1/.ssh/id_rsa\':,没有任何匹配的内容assword:,因此sshpass一直等待从未出现的默认提示。

\n

用于-P覆盖默认值。

\n
\n

-P
\n设置密码提示。Sshpass 在程序到 TTY 的输出中搜索此提示,作为何时发送密码的指示。默认情况下 sshpass 查找字符串 (与和assword: 匹配)。如果您的客户端提示不属于其中任何一个,您可以使用此选项覆盖默认值。Password:password:

\n
\n

(来源:man 1 sshpass

\n

在你的情况下,它可能是:

\n
sshpass -P assphrase -p "pass" ssh user1@10.7.175.143\n
Run Code Online (Sandbox Code Playgroud)\n

现在,如果sshpass拦截Enter passphrase \xe2\x80\xa6来自ssh,它将响应您在 后指定的任何内容-p。接下来,它将作为您的终端和正在使用的终端之间的中继ssh;它会变得透明。

\n

一般来说,sshpass可用于向通常使用终端(而不是 stdin+stdout+stderr)提示输入和读取密码的任何工具提供密码(一般为字符串)。-P允许您根据工具使用的提示调整命令。

\n
\n

1手册说assword:,但你的输出sshpass -vusing match "assword"。无论怎样,您都需要-P正确地传递密码。

\n