在“交互”之后expect脚本还能继续执行其他命令吗?
我正在编写一个脚本来运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,处理可以在[交互]后继续。
简短回答:将最后一个 {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?