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

pxssh submit su commands = very very slow

P: n/a
This works but after the su command you have to wait like 2 minutes
before each command gets executed ?
What did i do wrong ?

import pxssh

try:
s = pxssh.pxssh()
s.login ('127.0.0.1', 'gert', '123')
s.sendline ('uptime')
s.prompt()
print s.before
s.sendline ('ls -l')
s.prompt()
print s.before
s.sendline ('df')
s.prompt()
print s.before
s.sendline ('su')
s.expect('Password:')
s.sendline ('123')
s.prompt()
print s.before
s.sendline ('df')
s.prompt()
print s.before
s.sendline ('exit')
s.logout()

except pxssh.ExceptionPxssh, e:
print "pxssh failed on login."
print str(e)
Jun 28 '08 #1
Share this Question
Share on Google+
7 Replies

P: n/a
gert:
This works but after the su command you have to wait like 2 minutes
before each command gets executed ?
s.sendline ('su')
s.expect('Password:')
A common idiom seems to be to omit the start of the expected reply
since it may not be grabbed quickly enough. Then the prompt has to time
out. Try
s.expect('assword:')

Neil
Jun 28 '08 #2

P: n/a
On Jun 29, 1:19*am, Neil Hodgson <nyamatongwe+thun...@gmail.com>
wrote:
gert:
This works but after the su command you have to wait like 2 minutes
before each command gets executed ?
* * * * * * s.sendline ('su')
* * * * * * s.expect('Password:')

* * A common idiom seems to be to omit the start of the expected reply
since it may not be grabbed quickly enough. Then the prompt has to time
out. Try
* * * * * * * s.expect('assword:')
I tested, but it has the same result. Each command after the su
command needs 2 minutes before pxssh display's its output.
Could there be a expat loop in pxssh or something ?

Jun 28 '08 #3

P: n/a
On Jun 29, 1:44*am, gert <gert.cuyk...@gmail.comwrote:
On Jun 29, 1:19*am, Neil Hodgson <nyamatongwe+thun...@gmail.com>
wrote:
gert:
This works but after the su command you have to wait like 2 minutes
before each command gets executed ?
* * * * * * s.sendline ('su')
* * * * * * s.expect('Password:')
* * A common idiom seems to be to omit the start of the expected reply
since it may not be grabbed quickly enough. Then the prompt has to time
out. Try
* * * * * * * s.expect('assword:')

I tested, but it has the same result. Each command after the su
command needs 2 minutes before pxssh display's its output.
Could there be a expat loop in pxssh or something ?
I am pretty sure it has to do with the prompt changing from $ to #
Jun 29 '08 #4

P: n/a
On 2008-06-28, Neil Hodgson <ny*****************@gmail.comwrote:
gert:
>This works but after the su command you have to wait like 2 minutes
before each command gets executed ?
s.sendline ('su')
s.expect('Password:')

A common idiom seems to be to omit the start of the
expected reply since it may not be grabbed quickly enough.
Then the prompt has to time out. Try s.expect('assword:')
I don't see why the first letter of the password prompt would
be dropped. Tty drivers have buffered data for as long as I've
been using Unix (25+ years). After writing the username you
can wait for an hour before calling read(), and you'll still
see the entire password prompt. I always assumed it was done
that way so the script would work for either "password:" or
"Password:".

--
Grant Edwards grante Yow! Edwin Meese made me
at wear CORDOVANS!!
visi.com
Jun 29 '08 #5

P: n/a
this does the same except 100 times faster ?

I don't understand the logic about the prompt, its not the same as the
output from the bash shell ?

root@r12276:~# cat ssh2.py
import pexpect
import sys

child = pexpect.spawn("ssh ge**@127.0.0.1")
#child.logfile = sys.stdout

i = child.expect(['assword:', r'yes/no'],timeout=120)
if i==0:
child.sendline('123')
elif i==1:
child.sendline('yes')
child.expect('assword:', timeout=120)
child.sendline('123')
child.expect('g***@rxxxx.ovh.net: ~')
print child.before

child.sendline('ls -l')
child.expect('g***@rxxxx.ovh.net:')
print child.before

child.sendline('su')
child.expect('assword:')
child.sendline('123')
child.expect('g***@rxxxx.ovh.net: /srv/www/gert')
print child.before

child.sendline('ls -l')
child.expect('r***@rxxxx.ovh.net:')
print child.before

Jun 29 '08 #6

P: n/a
On Sat, 28 Jun 2008 19:08:59 -0700, gert wrote:
this does the same except 100 times faster ?

I don't understand the logic about the prompt, its not the same as the
output from the bash shell ?

root@r12276:~# cat ssh2.py
import pexpect
import sys

child = pexpect.spawn("ssh ge**@127.0.0.1") #child.logfile = sys.stdout

i = child.expect(['assword:', r'yes/no'],timeout=120) if i==0:
child.sendline('123')
elif i==1:
child.sendline('yes')
child.expect('assword:', timeout=120) child.sendline('123')
child.expect('g***@rxxxx.ovh.net: ~') print child.before

child.sendline('ls -l')
child.expect('g***@rxxxx.ovh.net:')
print child.before

child.sendline('su')
child.expect('assword:')
child.sendline('123')
child.expect('g***@rxxxx.ovh.net: /srv/www/gert') print child.before

child.sendline('ls -l')
child.expect('r***@rxxxx.ovh.net:')
print child.before
You could try changing the prompt (pxssh appears to have a way of doing
that), but I prefer to set up passwordless, passphraseless ssh and do
each command separately. For the rootly portions, you might look into
passwordless sudo if you go that route.

Here's something about setting up passwordless, passphraseless ssh:

http://stromberg.dnsalias.org/~strombrg/ssh-keys.html

Jun 29 '08 #7

P: n/a
On Jun 29, 4:45*am, Dan Stromberg <dstrombergli...@gmail.comwrote:
On Sat, 28 Jun 2008 19:08:59 -0700, gert wrote:
this does the same except 100 times faster ?
I don't understand the logic about the prompt, its not the same as the
output from the bash shell ?
root@r12276:~# cat ssh2.py
import pexpect
import sys
child = pexpect.spawn("ssh g...@127.0.0.1") #child.logfile = sys.stdout
i = child.expect(['assword:', r'yes/no'],timeout=120) if i==0:
* * child.sendline('123')
elif i==1:
* * child.sendline('yes')
* * child.expect('assword:', timeout=120) child.sendline('123')
child.expect('g...@rxxxx.ovh.net: ~') print child.before
child.sendline('ls -l')
child.expect('g...@rxxxx.ovh.net:')
print child.before
child.sendline('su')
child.expect('assword:')
child.sendline('123')
child.expect('g...@rxxxx.ovh.net: /srv/www/gert') print child.before
child.sendline('ls -l')
child.expect('r...@rxxxx.ovh.net:')
print child.before

You could try changing the prompt (pxssh appears to have a way of doing
that), but I prefer to set up passwordless, passphraseless ssh and do
each command separately. *For the rootly portions, you might look into
passwordless sudo if you go that route.

Here's something about setting up passwordless, passphraseless ssh:

http://stromberg.dnsalias.org/~strombrg/ssh-keys.html
My boss does not allow me to cp a key on the phone server. I only have
a email with some commands and passwords.
Jun 29 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.