rubberscissors
Technical User
This seems like it should be simple but for the life of me I can't seem to peg this; I'm using expect to telnet into a device and issue commands. What I want to do is then expect certain things in the output, and then execute other commands based on what it sees. The problem is, I need it to look for multiple things in the same output (call them klatu, barada, and nikto). So I tried the following:
expect "prompt" { send "the command\r" }
expect { klatu { incr klatucheck +1
exp_continue }
barada { incr baradacheck +1
exp_continue }
nikto { incr niktocheck +1
exp_continue }
}
Since initially I just wanted to check for certain conditions, then react to them individually afterwards. This didn't work, because it still only went through the output once and many 'niktos' were passed by during the 'barada' screening...so, I just wrote the output to a file and screened the file multiple times, which works, but I couldn't help but think the file wasn't necessary...
So, I decided to try it this way:
set matchlist { klatu barada nikto }
set x $expect_out(buffer)
set i 1
foreach item $matchlist {
set match$i 0
if [ string match "*$item*" $x ] { set match$i 1 }
incr i +1
}
if { $match1 != 0 } { klatucheck $x }
if { $match2 != 0 } { baradacheck $x }
if { $match3 != 0 } { niktocheck $x }
Then, I passed the expect output buffer to the different 'check' proceedures as a string. So far so good.
The problem arises when I then screen the output buffer in the 'check' proceedures. For my purposes, I already know at least one I/O slot was in the 'klatu' state. Now I want to know which I/O slots are in the 'klatu' state. I run the 'klatucheck' proceedure, and do the following:
if [ string match "*klatu*" $x ] {
set index [ string first "klatu" $x ]
set a [ expr $index - 8 ]
set b [ expr $index - 7 ]
set i [ string index $x $a ]
set o [ string index $x $b ]
set io$count $i$o
}
This returns a two digit number as $io1, the I/O slot which is in the 'klatu' state...unfortunately, it only grabs the first instance of 'klatu' so any other slots in that state are missed completely. I need it to be able to keep screening through the string until all instances have been found, then I'll compile the slot numbers into a list, then check the slots on the list using other commands.
Does anyone out there know how I can check the whole string instead of just stopping after the first match? Or have I just started a journey down the wrong path? Any help would be appreciated!
expect "prompt" { send "the command\r" }
expect { klatu { incr klatucheck +1
exp_continue }
barada { incr baradacheck +1
exp_continue }
nikto { incr niktocheck +1
exp_continue }
}
Since initially I just wanted to check for certain conditions, then react to them individually afterwards. This didn't work, because it still only went through the output once and many 'niktos' were passed by during the 'barada' screening...so, I just wrote the output to a file and screened the file multiple times, which works, but I couldn't help but think the file wasn't necessary...
So, I decided to try it this way:
set matchlist { klatu barada nikto }
set x $expect_out(buffer)
set i 1
foreach item $matchlist {
set match$i 0
if [ string match "*$item*" $x ] { set match$i 1 }
incr i +1
}
if { $match1 != 0 } { klatucheck $x }
if { $match2 != 0 } { baradacheck $x }
if { $match3 != 0 } { niktocheck $x }
Then, I passed the expect output buffer to the different 'check' proceedures as a string. So far so good.
The problem arises when I then screen the output buffer in the 'check' proceedures. For my purposes, I already know at least one I/O slot was in the 'klatu' state. Now I want to know which I/O slots are in the 'klatu' state. I run the 'klatucheck' proceedure, and do the following:
if [ string match "*klatu*" $x ] {
set index [ string first "klatu" $x ]
set a [ expr $index - 8 ]
set b [ expr $index - 7 ]
set i [ string index $x $a ]
set o [ string index $x $b ]
set io$count $i$o
}
This returns a two digit number as $io1, the I/O slot which is in the 'klatu' state...unfortunately, it only grabs the first instance of 'klatu' so any other slots in that state are missed completely. I need it to be able to keep screening through the string until all instances have been found, then I'll compile the slot numbers into a list, then check the slots on the list using other commands.
Does anyone out there know how I can check the whole string instead of just stopping after the first match? Or have I just started a journey down the wrong path? Any help would be appreciated!