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

subpatterns in Expect regexp and assigning to a variable

10 views
Skip to first unread message

brickwalls19

unread,
May 22, 2007, 1:21:38 PM5/22/07
to
I'm working on a small Expect/tcl script for logging into a switch. I
can't figure out how to do the following that's between the question
marks.

expect -re "(enable\\) \*$)|(> *\ *$)|(# *\ *$)"


????
what i want to do next --> if the prompt ends in either '(enable)',
'>', or '#', assign it to the variable "login".
????

switch -- $login {
"> *\ *$" {
send "en\r"
expect "ssword"
send "$password\r"
send "config t\r"
}
"enable\\) \*$" {
send "\n\r"
}
"# *\ *$" {
send "config t\r"
}
default {
send " \n"
}
}


Also if anyone can see a simpler way this can be done I'd appreciate
it. Definitely trying to use Expect/tcl to make my work assignments
easier and bearable. Also a learning opportunity.

Bruce Hartweg

unread,
May 22, 2007, 2:07:57 PM5/22/07
to
the expect_out array will contain the full match and submatches
so
set login $expect_out(1,string)
will do what you ask, but you can alos skip the double step of
matching a, b or c and the doing a switch on a, b or c

with the following

expect {
-re "> *\ *$" {


send "en\r"
expect "ssword"
send "$password\r"
send "config t\r"
}

-re "enable\\) \*$" {
send "\n\r"
}
-re "# *\ *$" {
send "config t\r"
}
timeout {
send " \n"
}
}


Bruce

brickwalls19

unread,
May 22, 2007, 3:16:56 PM5/22/07
to

brickwalls19

unread,
May 23, 2007, 11:35:18 AM5/23/07
to


Thanks Bruce. Your alternative works for me and straight forward.

Jeff

0 new messages