Thursday, March 11, 2010

expect

Expect keywords:
spawn
send
expect
interact
$expect_out(0,string)
$expect_out(buffer)
set timeout
expect eof


EXPECT EXAMLES: 
1. Pattern matching 
#!/usr/bin/expect
set timeout 15
expect "hi"  { send "You said hi\n" } \
     "hello"  { send "Hello to you\n" } \
     "bye"    { send "Goodbye\n" } \
     timeout  { send "I’m fed up\nbye\n" }


2. Expect script to connect to ssh server iteratively:
#!/usr/bin/expect -f
set timeout 3600
set iter 50
for {set i 1} {$i <= $iter} {incr i} {
        puts "SSh Iteration : $i \n"
        spawn ssh admin@192.168.1.4
        expect "Password:"
        send "mail4567\n"
        close $spawn_id
        after 900
}
 

3. Expect example
$ cat test.sh
#!/bin/sh
echo "Whats your name"
read name
echo "Hello $name"Here is expect script "test.exp" to automate the above script#!/usr/bin/expect -f

spawn ./test.sh

expect "Whats your name"
send "RAJA\r"
interact$ expect test.exp
spawn ./test.sh
Whats your name
RAJA
Hello RAJA

4. attaching to gdb server
#!/usr/bin/expect                                                              
spawn telnet 10.64.70.21                                                      
expect "$ " {send "gdbserver :1234 --attach \$(pgrep wncd)\n"}                 
interact


5. Attaching to gdb client:
#!/usr/bin/expect                                                              
spawn /auto/binos-tools/bin/mcp_gdb  -r $env(PWD) -p $env(PWD)/linkfarm/x86_64_cge7/usr/binos/bin/wncd
expect "\(gdb\) " {send "target remote 10.64.70.21:1234\n"}                   
interact  







References:

http://www.tcl.tk/man/expect5.31/expect.1.htmlhttp://www.tcl.tk/man/expect5.31/expect.1.html
http://unstableme.blogspot.com/search/label/Expect

http://floppsie.comp.glam.ac.uk/Glamorgan/gaius/scripting/5.html
http://www.thegeekstuff.com/2010/10/expect-examples/http://www.thegeekstuff.com/2010/10/expect-examples/

No comments:

Post a Comment