Help with expect expect_out


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with expect expect_out
# 1  
Old 03-28-2009
Help with expect expect_out

This is my simple expect scritpt:

#!/usr/bin/expect -f
match_max 100000
set timeout -1
spawn telnet $IP
expect "#"
send -- "shell\r"
expect "*Ready*"
send -- "init\r"
expect "*Ready*"
send -- "readsensor \r"
expect -- "*" <<< Output of this is a 2 digit number
set val $expect_out(buffer)
puts $val <<< This is empty
the 'shell' and 'init' works fine. when I get to readsensor, the output is a 2 digit number.. Even when I expect for * my expect_out(buffer) is empty..
Is there a better regex that I shd be using?
Please help me to figure this out. am I missing something... ? Please help..
# 2  
Old 03-28-2009
Please post the expected dialog. It would help us understand what you are trying to match. It would help also to use "exp_internal 1" to see how the matching goes.
# 3  
Old 03-28-2009
Thanks for looking into the script:

Let me tell you a little bit about what I am intending:

Based on the output of readsensor, if its > 10 do A, if it is < 10 do B.

I found one issue. I had set timeout as -1. Removed that, thought I was timing out before getting a response:

This is my script and output with exp_internal set to 1

Script and Logs:



cat checktemp.exp
#!/usr/bin/expect -f
set ipaddr [lrange $argv 0 0]
puts $ipaddr
match_max 100000
exp_internal 1
spawn telnet 192.168.137.1
expect "#"
send -- "shell\r"
expect "*Ready*"
send -- "init\r"
expect "*Ready*"
send -- "readsensor \r"
expect -- "*"
set val $expect_out(0,string)
expect "Ready*"
sleep 10s

puts "output is \n"
puts $val
send -- "quit\r"
expect "#"
send -- "exit\r"
expect eof



[root@localhost fix]# ./checktemp.exp

spawn telnet 192.168.137.1
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {10426}

expect: does "" (spawn_id exp6) match glob pattern "#"? no
Trying 192.168.137.1...

expect: does "Trying 192.168.137.1...\r\r\n" (spawn_id exp6) match glob pattern "#"? no
Connected to 192.168.137.1 (192.168.137.1).
Escape character is '^]'.

expect: does "Trying 192.168.137.1...\r\r\nConnected to 192.168.137.1 (192.168.137.1).\r\r\nEscape character is '^]'.\r\r\n" (spawn_id exp6) match glob pattern "#"? no


expect: does "Trying 192.168.137.1...\r\r\nConnected to 192.168.137.1 (192.168.137.1).\r\r\nEscape character is '^]'.\r\r\n\r\r\n" (spawn_id exp6) match glob pattern "#"? no



#
expect: does "Trying 192.168.137.1...\r\r\nConnected to 192.168.137.1 (192.168.137.1).\r\r\nEscape character is '^]'.\r\r\n\r\r\n\r\n\r\nBusyBox v1.2.1 (2009.03.26-17:02+0000) Built-in shell (msh)\r\nEnter 'help' for a list of built-in commands.\r\n\r\n# " (spawn_id exp6) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "Trying 192.168.137.1...\r\r\nConnected to 192.168.137.1 (192.168.137.1).\r\r\nEscape character is '^]'.\r\r\n\r\r\n\r\n\r\nBusyBox v1.2.1 (2009.03.26-17:02+0000) Built-in shell (msh)\r\nEnter 'help' for a list of built-in commands.\r\n\r\n#"
send: sending "shell\r" to { exp6 }

expect: does " " (spawn_id exp6) match glob pattern "*Ready*"? no
r
expect: does " r" (spawn_id exp6) match glob pattern "*Ready*"? no
dshell

expect: does " shell\r\n" (spawn_id exp6) match glob pattern "*Ready*"? no
[Ready]>
expect: does " shell\r\n[Ready]> " (spawn_id exp6) match glob pattern "*Ready*"? yes
expect: set expect_out(0,string) " shell\r\n[Ready]> "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) " shell\r\n[Ready]> "
send: sending "init\r" to { exp6 }

expect: does "" (spawn_id exp6) match glob pattern "*Ready*"? no
init

expect: does "init\r\n" (spawn_id exp6) match glob pattern "*Ready*"? no
init return status is 0

expect: does "init\r\ninit return status is 0\r\n" (spawn_id exp6) match glob pattern "*Ready*"? no
[Ready]>
expect: does "init\r\ninit return status is 0\r\n[Ready]> " (spawn_id exp6) match glob pattern "*Ready*"? yes
expect: set expect_out(0,string) "init\r\ninit return status is 0\r\n[Ready]> "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "init\r\ninit return status is 0\r\n[Ready]> "
send: sending "readsensor \r" to { exp6 }

expect: does "" (spawn_id exp6) match glob pattern "*"? yes
expect: set expect_out(0,string) ""
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) ""

expect: does "" (spawn_id exp6) match glob pattern "Ready*"? no
readsensor

expect: does "readsensor \r\n" (spawn_id exp6) match glob pattern "Ready*"? no
48

expect: does "readsensor \r\n48\r\n" (spawn_id exp6) match glob pattern "Ready*"? no
[Ready]>
expect: does "readsensor \r\n48\r\n[Ready]> " (spawn_id exp6) match glob pattern "Ready*"? yes
expect: set expect_out(0,string) "Ready]> "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "readsensor \r\n48\r\n[Ready]> "
output is


send: sending "quit\r" to { exp6 }

expect: does "" (spawn_id exp6) match glob pattern "#"? no
quit

expect: does "quit\r\n" (spawn_id exp6) match glob pattern "#"? no
#
expect: does "quit\r\n# " (spawn_id exp6) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "quit\r\n#"
send: sending "exit\r" to { exp6 }
exit
Connection closed by foreign host.
expect: read eof
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) " exit\r\nConnection closed by foreign host.\r\r\n"
write() failed to write anything - will sleep(1) and retry...
# 4  
Old 03-28-2009
You have two expect statements in a row so the second expect is not triggered. After the "ecpect -- "*"" line you need to send_user or just send "\r" so the next "expect "*Ready*" is triggered.

Code:
end -- "readsensor \r"
expect -- "*"
set val $expect_out(0,string)
expect "Ready*"

# 5  
Old 03-28-2009
Thanks for the response.. tried it and this is what I see
this is my script..

send -- "readsensor \r"
expect -- "*"
set val $expect_out(0,string)
send -- "\r"
expect "Ready*"
sleep 10s

puts "output is \n"
puts $val
send -- "quit\r"
expect "#"
send -- "exit\r"
expect eof



expect: does "readsensor \r\n\r\n[Ready]> command error ERR_UNKNOWN_CMD - command: \r\n\r\n" (spawn_id exp6) match glob pattern "Ready*"? yes
expect: set expect_out(0,string) "Ready]> command error ERR_UNKNOWN_CMD - command: \r\n\r\n"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "readsensor \r\n\r\n[Ready]> command error ERR_UNKNOWN_CMD - command: \r\n\r\n"
output is


send: sending "quit\r" to { exp6 }

expect: does "" (spawn_id exp6) match glob pattern "#"? no
49
[Ready]> [Ready]>
expect: does "49\r\n[Ready]> [Ready]> " (spawn_id exp6) match glob pattern "#"? no
quit

expect: does "49\r\n[Ready]> [Ready]> quit\r\n" (spawn_id exp6) match glob pattern "#"? no
#
expect: does "49\r\n[Ready]> [Ready]> quit\r\n# " (spawn_id exp6) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "49\r\n[Ready]> [Ready]> quit\r\n#"
send: sending "exit\r" to { exp6 }
exit
Connection closed by foreign host.
expect: read eof
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) " exit\r\nConnection closed by foreign host.\r\r\n"
write() failed to write anything - will sleep(1) and retry...
# 6  
Old 03-29-2009
Based on the error it appears that hitting the {return} key "\r" is not a valid command at the [Ready] prompt. If you do not want to send a command to the utility at his point because you need to wait on some response to come back the change the (send -- "\r") to send_user -- "Waiting on response from readsensor command...\r" NOTE the '\r' may be a '\n' it has been a while sense I wrote an Expect script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Save an specific part of a expect_out in a variable

I have a expect file like this #!/opt/tools/unsupported/expect-5.39/bin/expect spawn ssh -l user ip expect_after eof {exit 0} set timeout 10 log_file /report.txt expect "Password:" { send "pasword\r" } expect "$ " { send "date\r" } expect "$ " { send "readlink /somelink\r" } set... (7 Replies)
Discussion started by: bebehnaz
7 Replies

2. Shell Programming and Scripting

Trying to parse expect_out(buffer)

trying to telnet to a device, list the files, and delete them. I can get the script to telnet and log in OK, and even issue the command to list out the files. I can't figure out how to parse the expect_out(buffer) and extract the file name for use in the delete command. the files list out like... (4 Replies)
Discussion started by: imatinkerer
4 Replies

3. Shell Programming and Scripting

Jumbled output in expect_out(buffer)

I have a code like this : set ipv6_acl_max_chars test_acl_max_chars123456a789%s%d2345678ww134rt789qa23456789012345%c89012%a56789012x4r67890test_acl_max_chars1234567890.01234aabcdob34567aBC0 spawn telnet $myip expect "Login:" { send "admin\r" } expect "Password:" {send "admin\r" }... (0 Replies)
Discussion started by: ylucki
0 Replies

4. Programming

Flushing expect_out(buffer)

Can some one tell me how to flush expect_out(buffer)? below is my code expect -re {.*} {} expect "swpackages>*" send -i $con "trial.bat \r" set outcome $expect_out(buffer) expect "*continue*" set prevreport $expect_out(buffer) send "\r \r"; problem is :- I am getting "pre" stuffs... (0 Replies)
Discussion started by: cityprince143
0 Replies

5. Shell Programming and Scripting

Expect_out help!!!

I am trying to read a file via SSH connect and store it to expect_out(buffer). I am a virgin to expect. Help is really appreciated. Wasted almost a day :-( Code is as follows expect "system32>" send "type output.csv"; send "\r"; expect "system32>" set outcome $expect_out(buffer)... (2 Replies)
Discussion started by: cityprince143
2 Replies

6. Shell Programming and Scripting

Expect_out(buffer) works but it doesn't get all lines

Hello "expect" experts I am new at Expect. I have searched for a little while how to capture multiple lines with Expect and I am almost succeeded on that but I don't get all the lines of a command's output that the script executes on a server. Here is how my script works in a nutshell - ... (6 Replies)
Discussion started by: capacho6666
6 Replies

7. Shell Programming and Scripting

Parsing expect_out using regex in expect script

Hi, I am trying to write an expect script. Being a newbie in expect, maybee this is a silly doubt but i am stuck here. So essentially , i want the o/p of one router command to be captured . Its something like this Stats Input Rx : 1234 Input Bytes : 3456 My expect script looks ... (5 Replies)
Discussion started by: ashy_g
5 Replies

8. Shell Programming and Scripting

Problems with expect_out command

expect "#" send "terminal-length 0\r" expect "#" send "show mp cpu\r" expect "#" send "show mp memory\r" expect "#" while {1} { expect "#" send "clear counters\r" sleep 30 ... (0 Replies)
Discussion started by: roh_20022002
0 Replies

9. Shell Programming and Scripting

expect_out buffer no such variable running script background

I am trying to use send and receive using expect. the expect_out(buffer) is working fine while it is running it as foreground. But the same script when it is ran as background, the expect_out(buffer) errored out. Is there any factor influence when we run script in foreground and in background? ... (0 Replies)
Discussion started by: shellscripter
0 Replies

10. Shell Programming and Scripting

expect_out(buffer) empty

I have only some info into my buffer, but after a rssi command I see the folowing lines expected into buffer but not present : rssi=-106 rssi=-109 I see in my buffer only the first part of the output, here you are a part of script : #!/usr/bin/expect -f #global expect_out match_max 10000000... (1 Reply)
Discussion started by: ugobale
1 Replies
Login or Register to Ask a Question