【问题标题】:Linux Expect SSH hangsLinux Expect SSH 挂起
【发布时间】:2013-10-09 16:35:59
【问题描述】:

我尝试在一组计算机上安装 R,但有人告诉我检查 Expect。

我正在关注一个关于 Linux Expect Command 的教程,以自动化 ssh 到节点的过程并自动安装开源 R。

我有点卡在 ssh 部分: (Tutorial goes here)

#!/usr/bin/expect -f 

if {[llength $argv] != 3} {
puts "usage: ssh.exp username server password"
exit 1
}

set username [lrange $argv 0 0]
set server [lrange $argv 1 1]
set password [lrange $argv 2 2]

set timeout 60

spawn ssh $username@$server.mycompany.com

match_max 100000

expect "*?assword:*"

send -- "$password\r"

send -- "\r"

expect eof

我可以运行代码并登录到远程服务器,但是,我输入 ls 并且它只是挂在远程服务器端。然后我 ctrl + c 它注销并返回到我的主机服务器。

谁能告诉我登录后如何在Expect中继续这个过程。

更新:由于 Ireeder 的回答。

你只需要用interact替换expect eof,它就会把控制权交给用户。

【问题讨论】:

    标签: linux bash ssh expect


    【解决方案1】:

    interact 放在脚本的末尾,这会将控制权返回给脚本调用者,并允许您与远程shell 交互。所以像这样改变你的脚本:

    #... top of script
    send -- "$password\r"
    
    send -- "\r"
    
    interact
    

    http://wiki.tcl.tk/3914

    【讨论】:

    • 非常感谢它对我有用。你能解释一下这种情况下“expect eof”的作用吗?
    • 如果您想在进程关闭时进行一些后续操作,您只需要expect eof。当您手动退出生成的交互式 ssh 会话时,expect eof 无法正常工作。我不完全确定为什么,但我假设存在竞争条件,当您退出生成的 ssh 进程时,在期望查找 eof 时,生成的进程对于“期望的”不可用。
    • 我在 jenkins 中使用“expect eof”时遇到了上述问题。我不能使用交互,因为它在詹金斯中不起作用。我还有其他方法可以采取吗?
    猜你喜欢
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多