【问题标题】:[Expect]spawn id exp7 not open[期望]spawn id exp7 未打开
【发布时间】:2015-10-29 11:55:00
【问题描述】:

我试图让我的期望脚本读取文件,为文件的每一行运行一个命令并退出保存日志。代码如下:

#!/usr/bin/expect -f
set user [lrange $argv 0 0]
set password [lrange $argv 1 1] 
set ipaddr [lrange $argv 2 2]   
set arg1 [lrange $argv 3 3]
set systemTime [clock seconds]
set time [clock format $systemTime -format %a_%b_%d_%Y@%H'%M'%S]
set fp [open "$arg1" r]
set a "ssh-"
set b ".txt"
set s "_"
append newfile "${a}${arg1}${s}${time}${b}"
set timeout -1

spawn ssh $user@$ipaddr
match_max 100000
expect "*?assword:*"
send -- "$password\r"
log_file "$newfile" ;
expect "*#"
send_user "This is the $argv0 Script\r"
while {[gets $fp line] != -1} {
    send -- "scm $line\r"
    expect "*#"
}
close
send -- "exit\r"
expect eof

我的问题是,一旦到达文件末尾,我就会收到以下错误:

E6000_Lab_1# send: spawn id exp7 not open
    while executing
"send -- "exit\r""
    (file "filetest.tcl" line 28)

谁能帮我摆脱这个错误?

【问题讨论】:

    标签: tcl expect spawn


    【解决方案1】:

    很抱歉这样做,但似乎我自己又得到了答案。

    非常感谢所有回答这些问题并为解决这些问题提供一些想法的人。

    我的问题的解决方案是已打开文件的 id。一旦我关闭它,我的代码就停止崩溃了,代码如下:

    while {[gets $fp line] != -1} {
        send -- "scm $line\r"
        expect "*#"
    }
    close $fp
    send "ping xxx.xxx.xxx.xxx timeout 1 repeat-count 100\r"
    expect "# "
    send -- "exit\r"
    expect eof
    

    如您所见,“close”参数后的“$fp”允许我将下一个命令发送到循环之外并且不会出错。

    【讨论】:

      【解决方案2】:

      close 连接到子进程之后,您不能执行sendexpect

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-09-07
        • 1970-01-01
        • 1970-01-01
        • 2017-09-02
        • 1970-01-01
        • 1970-01-01
        • 2012-03-26
        相关资源
        最近更新 更多