Pexpect question.

P

Paul Lemelle

I am trying separate a script that users pexpect into
various functions within the same expect session. The
problem is that the function does not return control
back Main. Any insight into this issue would be
greatly appreciated. Below is sample code of the
problem.

Thanks,
Paul
____

import pexpect





def sshcon(user, password):

go = pexpect.spawn ('/usr/bin/ssh -l root %s '%
(user))

go.expect ('Password: ')

go.sendline (password)

go.interact()





#get node info for both clusters.



C1_node = raw_input("Enter the ip address for node on
cluster 1: ")

C1_pass = raw_input("Enter the password for the node
on cluster 1: ")





sshcon(C1_node, C1_pass)



#go to the path

chk.expect('# ')

chk.sendline('ls')

#chk = pexpect.spawn('ls') # veriy that you are
connected

chk.interact()



____________________________________________________________________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping
 
J

Jorgen Grahn

I am trying separate a script that users pexpect into
various functions within the same expect session. The
problem is that the function does not return control
back Main.

I do not understand what that sentence means.
Any insight into this issue would be
greatly appreciated. Below is sample code of the
problem.

First, what is the purpose of this program? It seems to be a more
tedious way to get an ssh login to some Unix host. Ssh can be
configured to do many things; maybe you do not need Python at all?

Second, it will fail as soon as you use the undefined object 'chk'.

Third, if "not return control back Main" means "sshcon() does not
return", then it is by design. You call go.interact(), which hands
over control to the user until he types an escape sequence. See the
pexpect documentation.

Fourth, sshcon() does not handle the full dialogue ssh can give you.
If you get "are you sure you want to connect" etc, you will hang until
pexpect.TIMEOUT is thrown.

I have reformatted the source code to be more readable:
import pexpect

def sshcon(host, password):
go = pexpect.spawn ('/usr/bin/ssh -l root %s ' % host)
go.expect ('Password: ')
go.sendline (password)
go.interact()

#get node info for both clusters.
C1_node = raw_input("Enter the ip address for node on cluster 1: ")
C1_pass = raw_input("Enter the password for the node on cluster 1: ")

sshcon(C1_node, C1_pass)

#go to the path
chk.expect('# ')
chk.sendline('ls')

chk.interact()

/Jorgen
 
J

Jorgen Grahn

Jorgen,

Thanks for your reply.

The ssh function is just a small part of what I would like to
accomplish. And yes, chk is undefined, I was trying to figure out why
control was not being returned from the sshcon funciton.

OK. Hope my note helped.
I looked for pexpect doucment on http://www.noah.org/wiki/Pexpect, but
the documentaiton link appear to be broken. Could you recommend
another site?

As I recall it, the online documentation is just the documentation
from the Python module itself, HTML-formatted. You can simply type
"pydoc pexpect" and get the reference manual. I think it's pretty
good.

/Jorgen
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top