472,991 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,991 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 8550
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.