behnaz behmanesh behnaz behmanesh -4 years ago 121
Linux Question

searching for a word in expect output

I have an expect command

expect "~]#" { send "virsh list --all\r"}


and the output would be

[root@lht1oneems-unit0 ~]# virsh list --all
Id Name State
----------------------------------------------------
399 lht1duplexvm-0 running
- rhelvm shut off


I want to use $expect_out(buffer) and have an if statement to do something if it finds running and do something else if not.
how can I parse into the result of $expect_out(buffer)?

Answer Source
expect "~]#"
send "virsh list --all\r"

# I assume another prompt follows this
expect "~]#"

if { [regexp {running} $expect_out(buffer)] } {
    do-something-for-running-process
} else {
    do-something-for-no-running-process
}

You could also do

if {[string first "running" $expect_out(buffer)] >= 0} {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download