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 something like this

  1 #!/usr/bin/expect -f
  2 set prompt {>}
  3 set timeout 20
  4 set user [lindex $argv 0]
  5 set password [lindex $argv 1]
  6 spawn su $user
  7 expect "Password:"
  8 send "$password\r";
  9 send -- "ls\r"
 10 expect $prompt
 11 puts "The output is $expect_out(buffer)"
 12
 13 interact

Now, i see the complete o/p by the last statement. Not able to get the correct syntax of -re for parsing the o/p. Just want to store the variables
Input Rx and input bytes.

Any help is really appreciated.

Thanks,
ashy_g

Please always use code tags when posting any code fragments or data samples. If you are not sure how to use code tags, then please have a look at this forum video tutorial

Can you please post expect o/p and what exactly do you want to extract from expect o/p in code tags.

Ok sure,

So the input values are somthing like this

# show traffic

--------------------------------------------------------------
                      Traffic Statistics
--------------------------------------------------------------
Packets Rx                   |               24644
alid Packets Rx              |               24644
Bytes                        |               99
Valid Byted                  |               45

and i want to write an expect script for login in the router, give the command to dislay these stats and then store them in the script variables to use them .

I am able to log-in the router and give the command but not able to use regex for parsing the expect_out buffer.

Any help is appreciated.

Thanks,
ashy_g

Try somthing like this:

#!/usr/bin/expect -f
set prompt {>}
set timeout 20
set user [lindex $argv 0]
set password [lindex $argv 1]
spawn su $user
expect "Password:"
send "$password\r";
send -- "ls\r"
 
expect -re "tics\r\n# -*\r\n(.*)\r\n(.*)$prompt"
 
send "exit\r\n"
expect eof
 
set values $expect_out(1,string)
set found [regexp { \| ([0-9]+)\r\n.* \| ([0-9]+)\r\n.* \| ([0-9]+)\r\n.* \| ([0-9]+)} $values match px vpx by vby]
if {$found == 1} {
    puts "px is $px"
    puts "vpx is $vpx"
    puts "by is $by"
    puts "vby is $vby"
} else {
    puts "No match found!"
}

Edit:

Of course the above is very dependant on your exact output and you may be best off playing around with a testing script to get the matching just right. Below is the script I used to test my posted solution, and you can see even without the device/server in question I can throw together some regex strings that match what you see comming from the device:

#!/usr/bin/expect -f
set values "# Packets Rx | 24644
# alid Packets Rx | 24644
# # Bytes | 99
# # Valid Byted | 45"
set found [regexp { \| ([0-9]+).* \| ([0-9]+)\n.* \| ([0-9]+)\n.* \| ([0-9]+)} $values match px vpx by vby]
if {$found == 1} {
    puts "px is $px"
    puts "vpx is $vpx"
    puts "by is $by"
    puts "vby is $vby"
} else {
   puts "failed to match anything from\r\n$values"
}

Thanks for the reply Chubler_XL, but it still doesnot solve my problem

So, essentailly, my script now looks something like this
--------------------------------------------------------------

#!/usr/bin/expect
spawn su admin
expect "abc>"
send "en\r"
expect "abc#"
sleep 1
send "show traffic \r"
send " "


set values $expect_out(buffer)

set found [regexp { \| ([0-9]+)\r\n.* \| ([0-9]+)\r\n.* \| ([0-9]+)\r\n.* \| ([0-9]+)} $values match px vpx by vby]
if {$found == 1} {
    puts "px is $px"
    puts "vpx is $vpx"
    puts "by is $by"
    puts "vby is $vby"
} else {
    puts "No match found!"
}

interact

---------------------------------------------------------

And the output of the command , "show traffic" is

abc#show  traffic

[ SLOT : 1 ]
--------------------------------------------------------------
                      Traffic Statistics
--------------------------------------------------------------
Input  Packets Rx                             |               29262
Input Module Valid Packets Rx             |               29262
Rx Packets                               |                 104
Tx Packets                               |                   0

and my script does not store the variables correctly :frowning:

Thanks,
Ashish

Try this one:

#!/usr/bin/expect
match_max 10000
set timeout 5
spawn su admin
expect "abc>"
send "en\r"
expect "abc#"
sleep 1
send "show traffic \r"
send " "
 
expect -re "tics\r\n---+\r\n(.*)\r\n(.*)abc#"
 
set values $expect_out(1,string)
set found [regexp { \| +([0-9]+)\r\n.* \| +([0-9]+)\r\n.* \| +([0-9]+)\r\n.* \| +([0-9]+)} $values match px vpx by vby]
if {$found == 1} {
    puts "px is $px"
    puts "vpx is $vpx"
    puts "by is $by"
    puts "vby is $vby"
} else {
    puts "No match found!\r\n$values"
}