Search code examples
tclexpectspawn

[Expect]spawn id exp7 not open


I am trying to get my expect script to read a file, run a command for every line of the file and exit saving the log. The code is below:

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

My problem is that once it comes to the end of the file i get the following error:

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

Can anyone help me get rid of this error please?


Solution

  • Sorry for doing this but it seems that I got the asnwer myself once again.

    Thanks very much for all of you who answer and provide some ideas to the resolution of these issues.

    The solution to my problem was on the id of the file that had been opened. Once I closed that, my code stopped crashing, the snipet is below:

    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
    

    As you can see, the "$fp" after the "close" argument allows me to send the next command out of the loop and without errors.