Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

parsing output from expect script

930 views
Skip to first unread message

MattB

unread,
Feb 8, 2012, 12:36:43 PM2/8/12
to
Hello,

My goal is to use expect to

1-connect to a series of systems
2-execute a command 20 times on each node to read through a log
(command is 'readlog')
3-grab the output of the commands
4-apply an regular expression on the returned buffer to only search
for lines with "Chassis"
5-prepend a hostname to each of the parsed lines returned from #4 like
nod200-bmc : <text after ran thru regex which contains "Chassis">

Here is what I have so far . This script does login ok and runs my
command to get logs but I see everything including the telnet
password send etc. In addition I also see a bunch of log info I do
not need.

===============

#!/usr/bin/expect -f
set expect_out(buffer) {}
set userid FOO
set passwd BAR
set timeout 10
match_max 100000
set nodecount 1
while {$nodecount < 3500} {
set node_init($nodecount) 1
log_user 0
spawn telnet nod$nodecount-bmc
expect "login : "
send -- "$userid\r"
expect -exact "USERID\r
Password: "
send -- "$passwd\r"
set count 20
while {$count > 0} {
send -- "readlog\r"
set count [expr $count -1]
#regular expression go here maybe?
}

send -- "exit\r"
expect "exit\r"
incr nodecount
}
send -- "exit\r"
expect eof

===============
Thank you for any insight.

re,

MattB

F. Michael Orr

unread,
Feb 9, 2012, 11:40:32 AM2/9/12
to
You have to clear the expect buffer after your commands if you don't want
their output. e.g.

send -- "$passwd\r"
expect -re "$passwd.*"prompt regex here" $"
set count 20 ....
send "readlog\r";
expect "readlog\r\n";
expect -re "output regex here"

You have to be careful with the regexs. Remember they are greedy by
default, which can sometimes match more than you want. Use the
'exp_internal 1' command at your regex; I find it useful to debug regex's.
0 new messages