473,699 Members | 2,302 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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('dis co')
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('dis co')
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 4618
On Feb 21, 6:13 pm, jonathan.s...@g mail.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('dis co')
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('dis co')
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...@g mail.com wrote:
On Feb 21, 6:13 pm, jonathan.s...@g mail.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,p expect.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('dis co')
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('dis co')
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...@gmai l.comwrote:
On Feb 21, 11:15 pm, jonathan.s...@g mail.com wrote:
On Feb 21, 6:13 pm, jonathan.s...@g mail.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,p expect.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('dis co')
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('dis co')
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...@gmai l.comwrote:
On Feb 23, 8:46 am, "amadain" <mfmdev...@gmai l.comwrote:
On Feb 21, 11:15 pm, jonathan.s...@g mail.com wrote:
On Feb 21, 6:13 pm, jonathan.s...@g mail.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,p expect.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('dis co')
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('dis co')
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
2262
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, the example script that looks most inclined to help me (by basically handling this situation,) "passmass.py" seems to fail, from whatever host to whatever host eg. $ ./passmass.py localhost Username: user1
2
5340
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 exceptions.OSError: (10, 'No child processes') in <bound method spawn.__del__ of <pexpect.spawn instance at 0x403d938c>> ignored What is happening and how do I get rid of the exception?
0
3108
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 wanted to make use of the ANSI.py module in the pexpect package to handle screen-based telnet sessions in Python, but I could never break the ice with the thing. After reading an article by Greg Jorgenson where he recounts using the package to...
5
4519
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 difficult it is do the following in python (without pexpect): - logon to a remote system using ssh - do an 'ls' and exit the remote shell - print the output from the remote shell session.
5
2731
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 dynamically through a web interface from a client machine. I use a bit of php to setup a simple form, and then have the php execute my python script on the server. The python script tries to 'su' to root to create the share, create dirs, set perms,...
1
10643
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 the error: #Begin Code import sys import pexpect
8
5866
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 * import pexpect import os
5
8628
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 I have done so far is spawned a child for ssh. 1) Now what do I do to execute shell_cmd(ls and grep), spawn another child? 2) Can I use the same child that was spawned for ssh, if so how?
2
5372
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 import os
0
8689
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8916
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7752
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6534
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2348
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.