gpt4 book ai didi

linux - 期望和 Bash 脚本

转载 作者:太空宇宙 更新时间:2023-11-04 09:23:57 25 4
gpt4 key购买 nike

我正在尝试执行一个带有循环的 expect 脚本,该循环尝试在 ssh 中连接并查看是否可行(良好的密码且可访问)。我试图将结果放入一个变量中,但只记录了 stdout 的末尾,而不是所有的 stdout。我怎么办?

result=$(
(/usr/bin/expect << EOF
spawn ssh $username@$ip -o StrictHostKeyChecking=no
set timeout 2
expect ":"
send -- "$password\r"
expect ">"
send -- "show clock\r"
expect ">"
EOF
) 2>&1)

谢谢。

最佳答案

如果您尝试使用 ssh 自动向另一台服务器发送命令:

  • 生成 ssh key ,而不是使用密码:

    ssh-keygen -t rsa -b 2048
  • 复制到服务器:

    ssh-copy-id id@server

现在您无需担心密码问题。 ssh key 用作密码,通常 更安全。有关 ssh key 的更多信息,请参阅这些(SSH login without passwordWhy is using SSH key more secure than using passwords?)。

然后你可以只使用这个命令 - 不需要 expect,因为你不会被要求输入密码。它将使用您的 ssh key 。我的位于 ~/.ssh/id_rsa。所以:

ssh id@server -i ~/.ssh/id_rsa "show clock;"
  • -i 代表身份文件,即您的ssh key 文件。

将向 SSH 服务器发送命令。

现在:

ssh-keygen -t rsa -b 2048
ssh-copy-id id@server
ssh id@server -i ~/.ssh/id_rsa "show clock;"

三个命令,你就搞定了!

关于linux - 期望和 Bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38792655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com