pexpect regex help

jonathan.sabo at gmail.com jonathan.sabo at gmail.com
Wed Feb 21 18:13:48 EST 2007


I have a pexpect script to walk through a cisco terminal server and I
was hoping to get some help with this regex because I really suck at
it.

This is the code:

        index = s.expect(['login: ', pexpect.EOF, pexpect.TIMEOUT])
        if index == 0:
            m = re.search('((#.+\r\n){20,25})(\s.*)',
s.before)                  #<---------- MY PROBLEM
            print m.group(3),
            print ' %s %s' % (ip[0], port)
            s.send(chr(30))
            s.sendline('x')
            s.sendline('disco')
            s.sendline('\n')
        elif index == 1:
            print s.before
        elif index == 2:
            print
            print '%s %s FAILED' % (ip[0], port)
            print 'This host may be down or locked on the TS'
            s.send(chr(30))
            s.sendline('x')
            s.sendline('disco')
            s.sendline('\n')

This is attempting to match the hostname of the connected host using
the output of a motd file which unfortunately is not the same
everywhere...  It looks like this:

#########################################################################
#       This system is the property
of:                                 #
#
#
#                DefNet
#
#
#
#       Use of this system is for authorized users
only.                #
#       Individuals using this computer system without authority, or
in #
#       excess of their authority, are subject to having all of
their   #
#       activities on this system monitored and recorded by
system      #
#
personnel.                                                      #
#
#
#       In the course of monitoring individuals improperly using
this   #
#       system, or in the course of system maintenance, the
activities  #
#       of authorized users may also be
monitored.                      #
#
#
#       Anyone using this system expressly consents to such
monitoring  #
#       and is advised that if such monitoring reveals
possible         #
#       evidence of criminal activity, system personnel may provide
the #
#       evidence of such monitoring to law enforcement
officials.       #
#########################################################################

pa-chi1 console login:

And sometimes it looks like this:

#########################################################################
#       This system is the property
of:                                 #
#
#
#                DefNet
#
#
#
#       Use of this system is for authorized users
only.                #
#       Individuals using this computer system without authority, or
in #
#       excess of their authority, are subject to having all of
their   #
#       activities on this system monitored and recorded by
system      #
#
personnel.                                                      #
#
#
#       In the course of monitoring individuals improperly using
this   #
#       system, or in the course of system maintenance, the
activities  #
#       of authorized users may also be
monitored.                      #
#
#
#       Anyone using this system expressly consents to such
monitoring  #
#       and is advised that if such monitoring reveals
possible         #
#       evidence of criminal activity, system personnel may provide
the #
#       evidence of such monitoring to law enforcement
officials.       #
#########################################################################
pa11-chi1 login:

The second one works and it will print out pa11-chi1  but when there
is a space or console is in the output it wont print anything or it
wont match anything...    I want to be able to match just the hostname
and print it out.

Any ideas?

Thanks,

Jonathan




More information about the Python-list mailing list