By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,500 Members | 2,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,500 IT Pros & Developers. It's quick & easy.

pexpect ssh login and ls | grep

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.