473,748 Members | 5,230 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 4626
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
2269
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
5343
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
3110
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
4525
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
2747
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
10646
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
5875
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
8632
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
5379
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
8991
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
8830
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
9544
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...
0
9372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9247
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
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
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.