Help with regex on telnet output

Adam Flott adam at npjh.com
Wed Jul 7 22:23:18 EDT 2004


On Tue, 6 Jul 2004 11:12:17 +0000 (UTC)
eddie at holyrood.ed.ac.uk (Eddie Corns) wrote:

> Adam Flott <adam at npjh.com> writes:
> 
> >I'm having some difficulty getting the expect function of telnetlib
> >to capture some data that gets returned from a telnet connection.
> 
> >Python's telnet debug reports this:
> 
> >recv 'whoami\n\r\xff\xfc\x01\r\nHi, my name is :     Home\r\nHere'
> >IAC WONT 1
> >recv ' is what I know about myself:\r\nModel:             '
> >recv '  VSX 7000\r\nSerial Number:       822232085C63K\r\nS'
> >recv 'oftware Version:    Release 1.0 - August 2001\r'
> >recv '\nBuild Information:   build at www.'
> >recv 'a.com\r\nFPGA Revision:       4.3.0\r\nMain Proc'
> >recv 'essor:      BSP15   v0.0 ~ Core/Mem Clks 415/111  '
> >recv '[3:4 0:3]\r\nTime In Last Call:   0:00:00\r\nTotal Tim'
> >recv 'e In Calls: 0:22:17\r\nTotal Calls:         18\r\nSNTP'
> >recv ' Time Service:   auto insync 21.1.1.1\r\nLocal Tim'
> >recv 'e is:        Mon,  5 Jul 2004 20:59:35 -0500\r\nNetw'
> >recv 'ork Interface:   ISDN_QUAD_BRI\r\nIP Video Number:  '
> >recv '   192.168.0.99\r\nISDN Video Number:   1.555.115047'
> >recv '4\r\nH323 Enabled:        True\r\nFTP Enabled:        '
> >recv ' True\r\nHTTP Enabled:        True\r\nSNMP Enabled:   '
> >recv '     True\r\nNIC Slot 1 SW Ver:   6.03\r\nNIC Slot 1 B'
> >recv 'oot Ver:   0.02\r\n\xff\xfb\x03'
> >IAC WILL 3
> >recv '\xff\xfc\x01'
> >IAC WONT 1
> >recv '\xff\xfb\x03'
> >IAC WILL 3
> 
> >The call I made to expect including a timeout of 1 seconds, which is
> >plenty of time to wait for a response, but keeps repeating WILL
> >3/WONT 1 indefinitely. 
> 
> >I only want to match from the begginning (\r\nHi. to the end
> >0.02\r\n). This data is not specific and I just want data that looks
> >like this format to be returned from the call to expect. The only
> >thing I've found that is constant is a colon separates one side to
> >the other.
> 
> >Here is the start to the regular expression I've been trying to get
> >to work:
> 
> >"^([\w\s]+?)(:)\s{1,}(.*):?\r\n$"
> 
> The first thing to note is that expect does not MATCH in quite the
> same sense as when matching a string.  What it does is match all the
> input from where it last left off until the point in the stream when
> the regex matches. eg:
> 
> if the stream looks like:
> 
>   "$ whereami\nYou are here.\n$ "
> 
> and you match on "a.e", it will return "$ whereami\nYou are".  The
> next read() or expect() call will start from "here.\n$ "
> 
> You would need to either pull out the portion you want from within
> that or more usually make sure you've matched up to the start. 
> Secondly the ^ and $ don't really make sense on a stream ($ will
> certainly cause it to hang; not sure why timeout doesn't fix that).
> 
> I generally try to use read_until() instead of expect(), this only
> works if you have well defined strings you can match on eg. "Hi, my
> name is" to get the start or better still match on the echo of the
> command you sent, and the user level prompt to finish (there doesn't
> seem to be a prompt in your trace though).  At worst you can use
> timeouts to know when it's done.
> 
> Fixed strings leads to more robust applications and easier to read
> code IMHO.
> 
> Eddie


After I read your message I finnally figured out a solution. Just match
the Hi part with the 6 or so variants that I know I will recieve. There
isn't a prompt as this is an embedded device and doesn't work like a
normal telnet login. The timeout issue is still a problem and I will
have to investigate that further. But for another day.

Thanks for the help, it is greatly appreciated.


Adam



More information about the Python-list mailing list