[预期]spawn id exp7 未打开

[Expect]spawn id exp7 not open

我正在尝试让我的 expect 脚本读取一个文件,运行 对文件的每一行执行命令并退出保存日志。代码如下:

#!/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)

谁能帮我解决这个错误?

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

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

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

我的问题的解决方案是在打开的文件的 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”允许我在循环外发送下一个命令而不会出现错误。