[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

pexpect ssh login and ls | grep

joemystery123

12/31/2007 7:40:00 PM

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
6 Answers

claird

12/31/2007 10:36:00 PM

0

In article <3eb81375-3e4a-4e0f-a4e7-bbb1d6cd0f8c@s19g2000prg.googlegroups.com>,
crybaby <joemystery123@gmail.com> wrote:
>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)
.
.
.
You might like to experiment with this:

import pexpect

prompt = '\$ '
filename = "mytest.log"
password = "xxxxxx"

child = pexpect.spawn('ssh -l %s %s ' (user, host))
child.expect([pexpect.TIMEOUT, '[Pp]assword: '])
child.sendline(password)
child.expect([pexpect.TIMEOUT, prompt])
child.sendline("ls %s > /dev/null 2>&1; echo $?" % filename)
child.expect([pexpect.TIMEOUT, prompt])
result = child.before
# You'll typically see "0" or "2" here, depending on
# whether filename exists or not.
print result.split('\r\n')[1]

Does this leave any questions?

joemystery123

1/1/2008 2:46:00 AM

0

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 ~]']

kar1107@gmail.com

1/1/2008 8:25:00 AM

0

On Dec 31 2007, 6:46 pm, crybaby <joemystery...@gmail.com> wrote:
> 1) what are these characters:
> \x1b]0;
> ~\x07\x1b[?1034h
>
> in line '\x1b]0;my@mycomp2:~\x07\x1b[?1034h[my@mycomp2 ~]'?

These are probably escape sequences in your shell prompt string.
Typically they are interpreted by the terminal, like xterm, to update
title bar.

>
> 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] ?

I don't think your expect worked fine to capture the desired output.

>
> 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

before/after makes sense only after an expect. At this point there is
only a sendline; not expect. So the above None output is expected.

> >>> child.expect([pexpect.TIMEOUT, '\$ '])
> 1

1 implies it matched the second pattern. You want to use raw strings.
r'\$' or else the re sent down is a plain $ which re interprets as end
of buffer.
Most important here is your prompt doesn't end with a $ (it's
something like [my@mycomp2 ~]). Thus make it,

child.expect(r'.*]')

Try the ls command and rest of the statements.

Karthik

> >>> 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 ~]']

Dikkie Dik

1/1/2008 4:49:00 PM

0

> 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.

joemystery123

1/1/2008 5:51:00 PM

0

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.

joemystery123

1/1/2008 7:24:00 PM

0

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