pexpect regex help

Discussion in 'Python' started by jonathan.sabo@gmail.com, Feb 21, 2007.

  1. Guest

    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
     
    , Feb 21, 2007
    #1
    1. Advertising

  2. Guest

    On Feb 21, 6:13 pm, wrote:
    > 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




    It is also posted here more clearly and formatted as it would appear
    on the terminal: http://www.pastebin.ca/366822
     
    , Feb 21, 2007
    #2
    1. Advertising

  3. amadain Guest

    On Feb 21, 11:15 pm, wrote:
    > On Feb 21, 6:13 pm, 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
     
    amadain, Feb 23, 2007
    #3
  4. amadain Guest

    On Feb 23, 8:46 am, "amadain" <> wrote:
    > On Feb 21, 11:15 pm, wrote:
    >
    >
    >
    > > On Feb 21, 6:13 pm, 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
     
    amadain, Feb 23, 2007
    #4
  5. amadain Guest

    On Feb 23, 8:53 am, "amadain" <> wrote:
    > On Feb 23, 8:46 am, "amadain" <> wrote:
    >
    >
    >
    > > On Feb 21, 11:15 pm, wrote:

    >
    > > > On Feb 21, 6:13 pm, 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




    sorry I just read that you are not matching sometimes. Try expecting
    for "ogin:" (without the first letter and trailing space). There could
    be no space after login: or there could be \t (tab).

    A
     
    amadain, Feb 23, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrei

    cannot get pexpect to work

    Andrei, Aug 29, 2003, in forum: Python
    Replies:
    5
    Views:
    561
    Andrei
    Aug 30, 2003
  2. Replies:
    3
    Views:
    797
    Reedick, Andrew
    Jul 1, 2008
  3. Hussein B

    A little help with pexpect

    Hussein B, Jul 19, 2009, in forum: Python
    Replies:
    2
    Views:
    895
    Piet van Oostrum
    Jul 19, 2009
  4. Saqib Ali
    Replies:
    1
    Views:
    176
    Nick Dokos
    Dec 20, 2011
  5. Saqib Ali
    Replies:
    1
    Views:
    367
    Roy Smith
    Dec 26, 2011
Loading...

Share This Page