473,413 Members | 2,043 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,413 software developers and data experts.

pexpect ssh login and ls | grep

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?

3) After executing the ls -l|grep mystest.log, how do I get the value
from pexpect?

shell_cmd = 'ls -l | grep mytest.log'
child = pexpect.spawn ('ssh my@mycomp2')
#child.sendline(shell_cmd)
>>child.sendline("ls")
3
>>print child.before
:~[
>>child.after
'my@mycomp2 '
>>child.sendline('/bin/bash', ['-c',shell_cmd])
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: sendline() takes at most 2 arguments (3 given)

thanks,
joe
Dec 31 '07 #1
5 8594
In article <3e**********************************@s19g2000prg. googlegroups.com>,
crybaby <jo***********@gmail.comwrote:
>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?

3) After executing the ls -l|grep mystest.log, how do I get the value
from pexpect?

shell_cmd = 'ls -l | grep mytest.log'
child = pexpect.spawn ('ssh my@mycomp2')
#child.sendline(shell_cmd)
>>>child.sendline("ls")
3
>>>print child.before
:~[
>>>child.after
'my@mycomp2 '
>>>child.sendline('/bin/bash', ['-c',shell_cmd])
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: sendline() takes at most 2 arguments (3 given)
Dec 31 '07 #2
1) what are these characters:
\x1b]0;
~\x07\x1b[?1034h

in line '\x1b]0;my@mycomp2:~\x07\x1b[?1034h[my@mycomp2 ~]'?

2) Also, how come I don't get 0 or 2(excuting ls command exit code)
from result.split('\r\n')[0] or result.split('\r\n')[1] ?

This is what I get:
>>import pexpect
child=pexpect.spawn('ssh my@mycomp2')
child.sendline("ls mytest.log /dev/null 2>&1; echo $?")
41
>>child.before
print child.before
None
>>print child.after
None
>>child.expect([pexpect.TIMEOUT, '\$ '])
1
>>result=child.before
print result.split('\r\n')[1]
[my@mycomp2 ~]
>>print result.split('\r\n')[0]
Last login: Mon Dec 31 20:52:09 2007 from com1
>>print result.split('\r\n')
['Last login: Mon Dec 31 20:52:09 2007 from com1\r',
'\x1b]0;my@mycomp2:~\x07\x1b[?1034h[my@mycomp2 ~]']

Jan 1 '08 #3
shell_cmd = 'ls -l | grep mytest.log'
child = pexpect.spawn ('ssh my@mycomp2')

I think you can give the ssh command an option to execute a file
remotely. That way, one command would be enough.
Jan 1 '08 #4
I don't get 0 or 2(excuting ls command exit code)
from result.split('\r\n')[0] or result.split('\r\n')[1]. I have to
try another method.

Regular shell ssh login:

[joe@com1 ~]$ ssh my@mycomp2
Last login: Mon Dec 31 20:51:09 2007 from com1
[my@mycomp2 ~]$

Pexpect Login:
>>import pexpect
child=pexpect.spawn("ssh my@mycomp2")
child.sendline("ls mytest.log /dev/null 2>&1; echo $? ")
42
>>child.expect([pexpect.TIMEOUT, r"\$"])
1
>>result1=child.before
result2=child.after
print result1
Last login: Tue Jan 1 11:22:05 2008 from com1
[my@mycomp2 ~]
>>print result2
$
>>print result1.split('\r\n')[1]
[my@mycomp2 ~]
>>print result1.split('\r\n')[0]
Last login: Tue Jan 1 11:22:05 2008 from com1

thanks.
Jan 1 '08 #5
I did try to excute the ssh and shell ls grep command in all in one
like so:

ssh my@mycomp2 "ls mytest.log /dev/null 2>&1; echo $?"

This seem to work, but also throwing exceptions. Also, including ssh
and shell command together would be a problem when I later add a pass
phrase to ssh key. Can someone provide little insight on this?

>>import pexpect
child=pexpect.spawn('ssh my@mycomp2 "ls mytest.log /dev/null 2>&1; echo $?"')

child.expect([pexpect.TIMEOUT, '\$'])
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1064, in
expect
return self.expect_list(compiled_pattern_list, timeout,
searchwindowsize)
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1132, in
expect_list
raise EOF (str(e) + '\n' + str(self))
pexpect.EOF: End Of File (EOF) in read_nonblocking(). Exception style
platform.
<pexpect.spawn object at 0xb7ea92ac>
version: 2.1 ($Revision: 395 $)
command: /usr/bin/ssh
args: ['/usr/bin/ssh', 'my@mycomp2', 'ls mytest.log /dev/null 2>&1;
echo $?']
patterns:
pexpect.TIMEOUT
\$
buffer (last 100 chars):
before (last 100 chars): 0

after: pexpect.EOF
match: None
match_index: None
exitstatus: 0
flag_eof: True
pid: 3524
child_fd: 3
closed: False
timeout: 30
delimiter: pexpect.EOF
logfile: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.1
delayafterclose: 0.1
delayafterterminate: 0.1
>>>
result=child.before
result2=child.after
print result
0
>>print result2
pexpect.EOF
Jan 1 '08 #6

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

Similar topics

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...
2
by: Adrian Casey | last post by:
I have a collection of tcl expect scripts which I am converting to python using the excellent pexpect module (http://pexpect.sourceforge.net/). So far I've had great success in getting all my...
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...
4
by: jonathan.sabo | last post by:
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() if index...
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...
0
by: Maximilian N. Andrews | last post by:
Dear All, I'm trying to write a script which will check the progress of my MD-simulations on a cluster. So far I've been able to ssh to each node and retrieve the data I was looking for by...
1
by: Sriram Rajan | last post by:
For some reason, Using pexpect causes my output to echo twice when I connect from my MAC Darwin (10.4) to Linux (CentOS release 5 ): The program: --------------------- #!/usr/bin/python #...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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...
0
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...

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.