Friday, July 1, 2011

cmds grouping in expect script if we are expecting on same patron

Hi, below script is just to explain a data variable that can be used as list. So each value in the list are send to spawned program. This is helpful only if you are expecting on same string.
#!/usr/bin/expect -f

set force_conservative 0 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg

set timeout -1
set data "ls
ls -ltr

#Spwan the new bash shell
spawn bash

match_max 100000

foreach line [split $data \n] {
expect "root@phaneedra:/home/phaneedra#"
send -- "$line\r"
sleep 1

Above foreach will split each line (each element) from data and copies to line variable. Now our script will look for "root@phaneedra:/home/phaneedra#" this and when ever it occurs send first and then next cmds.

This is just for example so it may not make much sense. But use full to test Cli interacting applications where expecting string patron not changed.

No comments: