【问题标题】:How to pass the variable to ssh command on shell如何在shell上将变量传递给ssh命令
【发布时间】:2020-07-13 10:07:21
【问题描述】:

我正在编写脚本来 ssh 服务器。

我有需要验证码和密码登录的服务器,我希望脚本可以每天登录服务器并在服务器上运行一些脚本(crontab)。

我只有服务器上的访问权限,我无权在服务器上设置 crontab。 所以我想在我自己的服务器上设置 crontab 和脚本来做。

(如果我不能在服务器上运行脚本,至少我想通过一行命令登录我的服务器。)

当前流量

user ~% ssh xxxx@example.com

The authenticity of host 'example.com' can't be established.
RSA key fingerprint is SHA256?[yyyyyyyyyyyyyyyyyyy].
Are you sure you want to continue connecting (yes/no/[fingerprint])? (yes)
Verification code:  (input)
Password: (input)

xxxx$ ./a.sh

我在下面写命令

print "(verification code)\n(pwd)\n" | ssh -t -t xxxx@example.com

我认为这个命令会将“code,enter,password”传递给ssh并执行一行命令登录。

但是输出是

Verification code: | (cursor)

任何人都可以帮我解决这个问题(让脚本自动按回车键)??

【问题讨论】:

    标签: linux ssh


    【解决方案1】:

    使用“sshpass”,例如:

    sshpass -p <password> ssh <username>@<remote-address> ./a.sh
    

    【讨论】:

    • 我不熟悉 sshpass,但我想您提供的示例还不够,因为显然除了密码之外还需要一个“验证码”。
    【解决方案2】:

    首先,如果“验证码和密码登录”是服务器中唯一的身份验证选项设置,我没有答案。

    由于您没有在问题中提及基于密钥的身份验证,我建议您调查是否可以选择。

    使用它为发送命令的机器生成私钥/公钥:

    ssh-keygen -t rsa
    

    这通常会在 ~/.ssh(id_rsa 和 id_rsa.pub)中创建密钥。

    然后,使用它来授权登录服务器:

    ssh-copy-id xxxx@example.com
    

    这会将您的公钥 (~/.ssh/id_rsa.pub) 添加到服务器中的 ~/.ssh/authorized_keys。

    【讨论】:

      猜你喜欢
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 2015-01-08
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      • 2021-11-30
      相关资源
      最近更新 更多