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.
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.
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.
#!/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"
}