Reputation: 751
I have an expect
script wrapped in a bash script for doing specific commands depending on whether an IP address is pulled from the Cisco router. The algorithm that I'm trying to code is something like the following:
send -h "ip int br"
expect -re {
if "192\.[0-9]{1,3}\.{2}[0-9]{1,3}"
{
expect
{
"up/up { do these things here }
"down/down" { do this instead }
}
}
else
{
send -h "<another command>"
expect
{
"up/up" { do this }
"down/down" { or this if down }
}
}
}
How can I do this? I have tried using expect
's equivalent of the switch
statements with the -ex
flag as suggested in an answer to another user's question (Making decisions on expect return) but haven't had any result with that.
Any ideas?
Upvotes: 0
Views: 243
Reputation: 247230
I would capture the returned address and then act on it. I see repeated code, so try to reduce that:
send -h "ip int br"
expect {
timeout {error "can't find an ip address after 'ip int br' command"}
-re {\m\d{1,3}(\.\d{1,3}){3}\M}
}
if { ! [regexp {192\.[0-9]{1,3}\.{2}[0-9]{1,3}} $expect_out(0,string)]} {
send -h "<another command>"
}
expect {
"up/up" { do these things here }
"down/down" { do this instead }
}
Other notes:
"192\.[0-9]{1,3}\.{2}[0-9]{1,3}"
will give you an error like invalid command name "0-9"
-- brackets are special to Tcl and need to be escaped unless they're in braces.Upvotes: 1