Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
trouble with expecting and regular expressions

Posted by doublea1535 (doublea1535), 23 August 2006
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

"$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.

Posted by doublea1535 (doublea1535), 24 August 2006
Just found this...

Changing the End-of-Line Character

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).

Okay, here is the output I am working with :


Serial5/0/1:0 is administratively down, line protocol is down
 Hardware is PA-MC-T3
 MTU 1500 bytes, BW 1536 Kbit, DLY 20000 usec, rely 255/255, load 1/255
 Encapsulation HDLC, crc 16, loopback not set
 Keepalive set (10 sec)
 Last input never, output never, output hang never
 Last clearing of "show interface" counters never
 Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0
 Queueing strategy: fifo
 Output queue: 0/40 (size/max)
 5 minute input rate 0 bits/sec, 0 packets/sec
 5 minute output rate 0 bits/sec, 0 packets/sec
    0 packets input, 0 bytes, 0 no buffer
    Received 0 broadcasts, 0 runts, 0 giants, 0 throttles
    0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort
    0 packets output, 0 bytes, 0 underruns
    0 output errors, 0 collisions, 0 interface resets
    0 output buffer failures, 0 output buffers swapped out
    0 carrier transitions alarm present
 Timeslot(s) Used: 1-24, subrate: 1536Kb/s, transmit delay is 0 flags
 non-inverted data


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 and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho