pxssh submit su commands = very very slow

G

gert

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

Neil Hodgson

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
 
G

gert

gert:


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

gert

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

gert

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 [email protected]")
#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('(e-mail address removed): ~')
print child.before

child.sendline('ls -l')
child.expect('(e-mail address removed):')
print child.before

child.sendline('su')
child.expect('assword:')
child.sendline('123')
child.expect('(e-mail address removed): /srv/www/gert')
print child.before

child.sendline('ls -l')
child.expect('(e-mail address removed):')
print child.before
 
D

Dan Stromberg

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 [email protected]") #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('(e-mail address removed): ~') print child.before

child.sendline('ls -l')
child.expect('(e-mail address removed):')
print child.before

child.sendline('su')
child.expect('assword:')
child.sendline('123')
child.expect('(e-mail address removed): /srv/www/gert') print child.before

child.sendline('ls -l')
child.expect('(e-mail address removed):')
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
 
G

gert

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 [email protected]") #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('(e-mail address removed): ~') print child.before
child.sendline('ls -l')
child.expect('(e-mail address removed):')
print child.before
child.sendline('su')
child.expect('assword:')
child.sendline('123')
child.expect('(e-mail address removed): /srv/www/gert') print child.before
child.sendline('ls -l')
child.expect('(e-mail address removed):')
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top