pexpect regex help

amadain mfmdevine at gmail.com
Fri Feb 23 03:53:38 EST 2007


On Feb 23, 8:46 am, "amadain" <mfmdev... at gmail.com> wrote:
> On Feb 21, 11:15 pm, jonathan.s... at gmail.com wrote:
>
>
>
> > On Feb 21, 6:13 pm, jonathan.s... at gmail.com wrote:
>
> > > I have apexpectscript 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
>
> > It is also posted here more clearly and formatted as it would appear
> > on the terminal:  http://www.pastebin.ca/366822
>
> what about using s.before.split("\r\n")[-1]?
>
> A



result=[x for x in s.before.split("\r\n") if x != ""]
print result[-1]

should cover the blank line problem

A




More information about the Python-list mailing list