I am having trouble getting expect to recognize a string with regexps. I am using the -re option. I am needing to match a line like:

Serial4/0:1 is administratively down, line protocol is down

The first string (Serial4/0:1) is defined in var $int. The rest could change though. I was trying something like:

expect -re "${int} is \[glob *]\r"

But that is not working.

Posted by admin (Graham Ellis), 23 August 2006

Try

"$int .*\r"

Posted by doublea1535 (doublea1535), 24 August 2006

I had tried that, but couldn't get it to catch only the one line that has that text in it. When I use that regex it is grabbing the entire output. I also tried /n, /x0A, /x03, and /x0D with no luck. This is a Cisco router that I am working with. I am going to do some more looking into this and will post back when I find an answer.

The end of each line typed at the terminal is ended with a CR+LF (Carriage Return plus Line Feed) signal. The CR+LF signal is sent when a user presses Enter or Return. To cause the current terminal line to send a CR signal as a CR followed by a NULL instead of a CR followed by a line feed (LF), use the following command in EXEC mode:

Command: Router> terminal telnet transparent

Purpose: Causes the current terminal line to send a CR signal as a CR followed by a NULL instead of a CR followed by an LF.

This command ensures interoperability with different interpretations of end-of-line handling in the Telnet protocol specification.

Posted by admin (Graham Ellis), 24 August 2006

I hope that change you're making doesn't effect other users of the router - it always worries me when people change the data that's coming in to suit their program.

As a general rule with expect (on which we're getting a lot of questions at the moment!), analyse the return sequence very carefully and remember that you're looking for a unique sequence in the return string to latch onto. If you come up with a sequence that's repeated, or wild carded at the end, you'll get variable results ....

Posted by doublea1535 (doublea1535), 1 September 2006

Okay, I believe I have a solution to my problem. Sorry for the delay, got sidetracked on other issues.

Nah, I won't be changing the router to suit the script, there are too many different routers the script will run against (plus that doesn't sound very scalable anyway).

What I need is the first line and only the first line so I can parse L1/L2 status. Here is the string I was able to use to do this :

set int Serial5/0/1:0...expect "$int is * \r"

Subsequently, the following has my line :

puts $expect_out(1,string)

Serial5/0/1:0 is administratively down, line protocol is down

What has me confuzzled is that I if I bump the * right next to \r it catches ALL of the output, not just the first line (this is part of what was messing me up).

Can you provide any insight into why that is Graham? I'm at work now and the only sessions I can open with our routers are SSH so I can't peek at the data, but when I get home I will run ethereal and see if there is a space at the end of the line before the \r character. But even if that is the case, shouldn't * match the space?

Posted by admin (Graham Ellis), 1 September 2006

With *\r (no space) it matches up to the latest line it happens to have received back by the time it gets around to checking, but with * \r (with a space) it will match us to the last SPACE TERMINATED line it has received. It's always a good idea to expect something that's going to be unique.

This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.