cerr cerr - 4 months ago 7
Bash Question

how do I use expect along with a bash script

I have a text file with a bunch of operations that i would like to send using my expect script which looks like (replacing op1 & op2 with the commands in my textr file, one command per line):

#!/usr/bin/expect
spawn ssh root@ccTest telnet jpaxdp
expect {\-> }
send "op1(20)\r"
expect {\-> }
send "op2\r"
expect {\-> }
send "exit\r"
expect eof


How can I do this?

Answer

I don't think I understand your question. It seems to me the easiest thing to do is to let expect handle reading the file:

#!/usr/bin/expect
set command_file [lindex $argv 0]
spawn ssh root@ccTest telnet jpaxdp
expect {\-> }

set fh [open $command_file r]
while {[gets $fh line] != -1} {
    send "$line\r"
    expect {\-> }
}
close $fh

send "exit\r"
expect eof

Then

Comments