Talespin_Kit
Talespin_Kit

Reputation: 21877

expect program send commands from file

How to make send command of "expect" program to read from a file and use each line as argument.

I want to use a loop like structure in expect program which may look like below(NOTE:- while loop is imaginary.)

spawn /my/program

expect {
 -re EBtxjjmEcQTxc0SLd4TdXxjUduxCOLZBwEme2Z.*password: { 
             while read_line in FILE; 
             do 
                 send $read-line; 
             done 
}

How to program the while-loop part equivalent using "expect"

Upvotes: 1

Views: 3760

Answers (1)

glenn jackman
glenn jackman

Reputation: 246774

Note in your question, you were missing a close brace, and you mis-typed your variable name (read_line and read-line)

Expect is a Tcl extension, so you have all the Tcl commands at your disposal

spawn /my/program
expect { 
    -re EBtxjjmEcQTxc0SLd4TdXxjUduxCOLZBwEme2Z.*password: { 
        set fh [open FILE r]
        while {[gets $fh read_line] != -1} {
            send "$read_line\r"
        } 
        close $fh
    }
}

If you install tcllib, you can do

package require fileutil
spawn /my/program
expect { 
    -re EBtxjjmEcQTxc0SLd4TdXxjUduxCOLZBwEme2Z.*password: { 
        fileutil::foreachLine read_line FILE {
            send "$read_line\r"
        } 
    }
}

Upvotes: 4

Related Questions