473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

pexpect regex help

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 '07 #1
4 4587
On Feb 21, 6:13 pm, jonathan.s...@gmail.com 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 '07 #2
On Feb 21, 11:15 pm, jonathan.s...@gmail.com wrote:
On Feb 21, 6:13 pm, jonathan.s...@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

Feb 23 '07 #3
On Feb 23, 8:46 am, "amadain" <mfmdev...@gmail.comwrote:
On Feb 21, 11:15 pm, jonathan.s...@gmail.com wrote:
On Feb 21, 6:13 pm, jonathan.s...@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

Feb 23 '07 #4
On Feb 23, 8:53 am, "amadain" <mfmdev...@gmail.comwrote:
On Feb 23, 8:46 am, "amadain" <mfmdev...@gmail.comwrote:
On Feb 21, 11:15 pm, jonathan.s...@gmail.com wrote:
On Feb 21, 6:13 pm, jonathan.s...@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


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

Feb 23 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andrei | last post by:
Hi, I'm at a crucial stage in the development of a wide area (multi-host) ssh wrapper, and need to control what happens when a host requires password authentication. After installing pexpect,...
2
by: Michael Surette | last post by:
I have been trying to automate the changing of passwords using python and pexpect. I wrote a script as a test and it works, except that it gives me an exception when it stops running: Exception...
0
by: Eric Myers | last post by:
Hello folks: (This message is also posted on the help forum at the pexpect sourceforge page, but all indentation in the code got stripped away when I submitted the post.) For some time I've...
5
by: funkyj | last post by:
I love pexpect because it means I may never have to use expect again (I don't do any heavy expect lifting -- I just need simple tty control)! As a python advocate I find it embarassing how...
5
by: half.italian | last post by:
Hi all. I try not to post until I am stuck in hole with no way out. I fought with this for several hours, and am currently in the hole. I'm doing a proof of concept for creating afp shares...
1
by: Kevin Erickson | last post by:
Hello, I am attempting to use pexpect in python to copy files from a server using scp; the copy works however exceptions are thrown and it exits unsuccessfully. Below is the a sample code and...
8
by: asgarde | last post by:
hello, I'm new in Python and i would like to use Pexpect to execute a root command (i want to mount via a Pyhton script a drive) so that's my script for the moment : from os import *...
5
by: crybaby | last post by:
I need to ssh into a remote machine and check if mytest.log file is there. I have setup ssh keys to handle login authentications. How do I determine if mytest.log is there by using Pexpect. What...
2
by: yellowblueyellow | last post by:
Hey , I need to SSH into a server .. (10.8.42.38) using pexpect the username is 'admin' and password is 'abc123' so far i have the following code import pexpect import sys import time...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.