在“交互”之后expect脚本还能继续执行其他命令吗?

发布于 2024-12-06 21:08:36 字数 389 浏览 0 评论 0原文

我正在编写一个脚本来运行 ssh 以登录远程主机,完成所有操作后,我输入 exit 并注销。但我希望脚本继续运行并在本地主机上写入日志。该脚本类似于:

#!/usr/bin/expect
spwan ssh qwerty@remote_host

expect {
    "password:" {
        send "123123\r"
    }
}

interact;
send "echo $(date) >> login_history.log\r"

但最后一个命令“发送...”总是失败,并显示错误消息,例如 “发送:spawn id exp4 not open ...”

当我从远程主机注销时,expect脚本可以像在本地主机上运行一样继续工作吗?

I am writing a script to run ssh so as to login a remote host, after all the operation is done, I type exit and log off. But I want the script to continue running and write log on the local host. The script is something like:

#!/usr/bin/expect
spwan ssh qwerty@remote_host

expect {
    "password:" {
        send "123123\r"
    }
}

interact;
send "echo $(date) >> login_history.log\r"

But the last command "send ..." always failed with the error message like
"send: spawn id exp4 not open ..."

When I log off from the remote host, can the expect script continue to work as it is running on the local host?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手心的海 2024-12-13 21:08:36

是的,处理可以在[交互]后继续。

简短回答:将最后一个 {send ...} 更改为 {exec date >>> 为了实现您想要的控制流,

您需要了解几个概念。首先,http://www.cotse.com/dlf/man/expect/interact_cmd_desc .htm 提供了简洁的概要和中间[交互]使用的示例。

第二:为什么你看到消息“...spawn id...not open...”?因为spawn id没有开放。您编写的脚本实际上表示“进行交互,然后在交互结束后,向 ssh 进程发送一个新命令”。如果您已经注销,那么,失效进程的 ID 当然不再可用。

第三:如何实现自己想要的目标?我不确定你想要什么。 听起来好像您只需按照我上面的描述来转换 [send] 就足够了。您觉得怎么样?

YES, processing can continue after an [interact].

Short answer: change the last {send ...} to {exec date >> login_history.log}

There are several concepts you'll want to understand to achieve the control flow you're after. First, http://www.cotse.com/dlf/man/expect/interact_cmd_desc.htm provides a succinct synopsis and example of intermediate [interact] use.

Second: why did you see the message "... spawn id ... not open ..."? Because the spawn id is not open. The script you wrote said, in effect, "interact, then, after interact is over, send a new command to the ssh process." If you've already logged out, then, of course that id for a defunct process is no longer available.

Third: how do you achieve what you want? I'm unsure what you want. It sounds as though it would be enough for you simply to transform the [send] as I've described above. How does that look to you?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文