Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > pexpect regex help

Reply
Thread Tools

pexpect regex help

 
 
jonathan.sabo@gmail.com
Guest
Posts: n/a
 
      02-21-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

 
Reply With Quote
 
 
 
 
jonathan.sabo@gmail.com
Guest
Posts: n/a
 
      02-21-2007
On Feb 21, 6:13 pm, (E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
amadain
Guest
Posts: n/a
 
      02-23-2007
On Feb 21, 11:15 pm, (E-Mail Removed) wrote:
> On Feb 21, 6:13 pm, (E-Mail Removed) 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

 
Reply With Quote
 
amadain
Guest
Posts: n/a
 
      02-23-2007
On Feb 23, 8:46 am, "amadain" <(E-Mail Removed)> wrote:
> On Feb 21, 11:15 pm, (E-Mail Removed) wrote:
>
>
>
> > On Feb 21, 6:13 pm, (E-Mail Removed) 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

 
Reply With Quote
 
amadain
Guest
Posts: n/a
 
      02-23-2007
On Feb 23, 8:53 am, "amadain" <(E-Mail Removed)> wrote:
> On Feb 23, 8:46 am, "amadain" <(E-Mail Removed)> wrote:
>
>
>
> > On Feb 21, 11:15 pm, (E-Mail Removed) wrote:

>
> > > On Feb 21, 6:13 pm, (E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing the system clock with pexpect confuses pexpect! Saqib Ali Python 1 12-26-2011 01:51 PM
Need help with really elementary pexpect fragment Saqib Ali Python 1 12-20-2011 05:31 AM
A little help with pexpect Hussein B Python 2 07-19-2009 05:08 PM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
cannot get pexpect to work Andrei Python 5 08-30-2003 07:20 PM



Advertisments