Expect script too fast: add a sleep between each line read from a file

43,488

Solution 1

So, older discussion but after wrestling with expect over the past day or two and gathering several helpful hints from users here and elsewhere I decided to post what I had found. I am also on a Mac not native Linux for this so some things were quirky. This script is called from a bash script with:

expect -d <filename>.expect

If #!<PATH to expect> -f is implemented in the top line of your *.expect file AND you:

chmod +x <filename>.expect your file this will work. The -d is for additional debug info.

Call expect with: expect -df <filename>.expect in your bash script to accomplish the same effect and you do not need executable rights on the file.

The debug info is very helpful on seeing your expect statements, variables, etc. like this:

spawn <program> <args>
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {29747}

expect: does "" (spawn_id exp10) match glob pattern "Please enter 
passphrase:"? no
Please enter passphrase:
expect: does "Please enter passphrase: " (spawn_id exp10) match glob 
pattern "Please enter passphrase:"? yes
expect: set expect_out(0,string) "Please enter passphrase:"
expect: set expect_out(spawn_id) "exp10"
expect: set expect_out(buffer) "Please enter passphrase:"

Here is the short bash script, (only an example, but helpful if you need it to do more complex stuff or just call it from bash for some reason)

#!/bin/bash

expect -d exp.expect "$WORD"
RET=$?

if [ $RET -eq 1 ]; then
    #mac specific, sheer laziness, allows me to do other stuff...
    #use esay or similar on Linux
    say "Query was found, expect returned success" 
    echo *************************************************** 
    echo ******************FOUND!***************************
fi
exit 0

Here is the expect script:

#!/usr/bin/expect -f
#capture logs for debugging
log_file -a log_file.txt

#dont timeout, let the program run its course, (was mandatory for me)
set timeout -1;

#capture all of our output for debugging
set output [ open "output.txt" "RDWR" ];

#This procedure is called repeatedly with the next word
proc check_word {w} {

#kickoff our other program we are going to talk to
spawn <program> <args>

#we want this one to go regardless, the next 2 are mutex
expect "Please enter passphrase:" { send "$w\r"; send_user "\nSENDING: $w\r"; send_user "\nOutput BUFFER: $expect_out(buffer)\n" }

#These are mutually exclusive, either worked or not, can be grouped
expect {
"scrypt: Passphrase is incorrect" { send_user "\n*FAILED*\n"; close }
-re {anders} { send_user "$expect_out(buffer)\n"; close; exit 1 }
}
#wait for the process to end, timeout is set to never expire
wait
}

#open the file to take the words from, (happens before proc above)
set input [ open "words.txt" "RDONLY" ];

#while there are still words, (we exit on a match) ...keep going....
while {[gets $input word] != -1} {
        check_word $word;
}
#close input file, TODO do this upon success in expect call too?
close $input
close $words

#EOF

Hopefully this helps some/anyone save some time out there!

Solution 2

Use a while loop:

set fp [open "datfile"]
while {[gets $fp line] >= 0} {
  puts $line
  #sleep 3
  # but better to wait for the prompt
  expect #
}
Share:
43,488

Related videos on Youtube

2legit2quit
Author by

2legit2quit

Updated on September 18, 2022

Comments

  • 2legit2quit
    2legit2quit almost 2 years

    I am trying to automate switch commands. It's almost all good, but when the expect script reads each line from the file containing my switch commands (listed line by line), the switch seems to stop after about 10 or 15 commands, i think the buffer is too small.

    How can I add in a sleep between each command that is read from the file? Thanks!!

    set fp [open "/home/room.txt" r]
    set data [read $fp]
    
    set timeout -60
    spawn telnet 10.91.60.14
    match_max 100000
    sleep 2
    expect *
    send -- "^Y"
    sleep 2
    send -- "password\r"
    sleep 2
    send -- "^[\[A"
    send -- "^[\[A"
    send -- "\r"
    sleep 1
    send -- "enable\r"
    send -- "configure terminal\r"
    sleep 1
    expect *
    sleep 2
    **send -- "$data"**
    sleep 2
    interact
    
    • foxfabi
      foxfabi over 8 years
      Instead of sleeping between sends, you should be expecting a pattern to appear first. For instance, between sending commands to your switch, you should expect to see the command prompt. It's often helpful to record a manual session with autoexpect.
    • 2legit2quit
      2legit2quit over 8 years
      Hi Glenn okay thank you. so I know I just need a "#" for expect since thats the switch prompt, but, if I add an expect before the send $data, will it still wait for the prompt for each line from the file?
    • foxfabi
      foxfabi over 8 years
      No, you'll want to follow thrig's solution.
  • foxfabi
    foxfabi over 8 years
    OK, I see what's happening: read does slurp the entire file. However that data is the 2nd argument to foreach, so it gets implicitly treated like a list. The data is split on whitespace and foreach is iterating over the words of the file, not the lines. If the file contains an unmatched open brace, you'll see errors about an invalid list.
  • thrig
    thrig over 8 years
    @glennjackman okay, I think I've beaten the code into line-reading-but-not-the-implicit-blank-line-after-the-ultim‌​ate-newline shape.
  • foxfabi
    foxfabi over 8 years
    That's what read -nonewline is for.
  • foxfabi
    foxfabi over 8 years
    OK, now: which do you prefer: the foreach loop or the while loop?
  • 2legit2quit
    2legit2quit over 8 years
    :/ :O uf. i have so much to learn. im lost. ill have to read through this properly..