Expect Scripting Logging not finishing

2019-09-09 10:09发布

问题:

I have an expect script that supplies configuration commands from a file to a router. I log the process to a file however expect stops writing to the log before the end of the configuration file. For instance if the config file has

q
r
s
t
u
v
w
x
y
z
eof

The log will have all the interaction up to u but the rest is not in the log but it appears the process finished all the way to z. The usual end of file comparison does not seem to work so I tried the following.

    set ok 0;
    while { $ok == 0 } {
      set line [ gets $config ];


       expect  { 
            "#" { send -s "$line\r"; }
            -re "\[.]" { send -s "$line\r"; }
        }
         if { $line == "eof" } {
           set ok 1;
        } else { }

   }
   close $config

It gets to setting ok to 1 and leaves the while statement but the script stops logging before that.

回答1:

Is it taking too long to complete? Reason I ask is maybe it is timing out due to not matching either option in your expect statement (first option matches a '#' prompt... what is the second option supposed to match?), and in that case, after expect times out in each pass, code would still sweep through all lines in your config file, and eventually reach eof and set ok to 1.

Here's what I would do:

  • include expect_internal 1 somewhere at the beginning of your script, so as to get a clue of what is going on
  • remove the semicolons after your set and send statements... afaik expect does not use those (I thought it would have complained!)
  • add a timeout option (a 3rd option) to your expect statement, and maybe print something accordingly if it gets there

I assume the list of commands you say is "q r s t u v w x y z eof" is not a one-liner but one command per line, correct?

Also, I'm assuming you have a set config [open "/path/to/filename.txt"] somewhere at the top of your script, and you are not showing it in your snippet there?

Feel free to post whatever you get after adding exp_internal 1 if you need help with that.



标签: expect