Tcl [预期]生成id exp7未打开

Tcl [预期]生成id exp7未打开,tcl,expect,spawn,Tcl,Expect,Spawn,我试图让我的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 $sy

我试图让我的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
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
我的问题是,一旦文件结束,就会出现以下错误:

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

有人能帮我摆脱这个错误吗?

在关闭与子进程的连接后,您不能执行
发送
期望

很抱歉这样做,但我似乎又一次得到了asnwer

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

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

#!/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
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”允许我将下一个命令发送出循环,并且没有错误