如何通过 SSH 执行远程 shell 脚本并通过该脚本中需要密码的命令提示输入密码?

Mat*_*ers 4 shell bash ssh shell-script

我想做这样的事情

ssh user@remote-domain.com ./remote_script.sh
Run Code Online (Sandbox Code Playgroud)

remote_script.sh 的内容

#!/bin/bash
hg pull
Run Code Online (Sandbox Code Playgroud)

这个例子比我实际做的要简单得多。我知道我可以直接通过 ssh 传递 shell 命令,但假设我想运行一个远程脚本。我也知道 Mercurial 有钩子,但也忽略它,为了简洁起见,我在这里仅使用 Mercurial 作为示例。

这是我期望发生的,用户的密码提示:

user@repository-domain.com password: 
Run Code Online (Sandbox Code Playgroud)

这是输出:

ssh user@remote-domain.com ./remote_script.sh
remote: Permission denied, please try again.
remote: Permission denied, please try again.
remote: Permission denied (publickey,password).
abort: no suitable response from remote hg!
Run Code Online (Sandbox Code Playgroud)

远程 shell 会话似乎正在向密码输入提示提供一些信息,并且在 3 次自动尝试后失败。我真的很想提示到达我的本地 shell 会话。也许那是不可能的。

我不想向存储库提供远程服务器密钥身份验证,我希望每次都提示用户。有没有办法做到这一点?

Mad*_*ist 9

很难从这篇文章中弄清楚到底发生了什么。密码提示是否由 hg 命令打印(我不熟悉 hg)?

我建议您尝试将-t选项添加到 ssh:

ssh -t user@remote-domain.com ./remote_script.sh
Run Code Online (Sandbox Code Playgroud)