pexpect regex help

J

jonathan.sabo

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
 
J

jonathan.sabo

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
 
A

amadain

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?

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
 
A

amadain

On Feb 21, 6:13 pm, (e-mail address 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
 
A

amadain

On Feb 21, 11:15 pm, (e-mail address removed) wrote:
On Feb 21, 6:13 pm, (e-mail address 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]?

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top