On 19 July 2011 13:20, Johan Geldenhuys <jo...@accesstel.com.au> wrote:

It works fine for me in an interactive idle session.. Have you tried
this to see if works like this? Does the command work when run
directly from the command line?

> I am using pexpect in a script to SCP files to a inux server.
> Here is a snippet from the code:
>
> def doScp(self, user, password, host, path, files):
>
>     fNames = " ".join(files)
>     self.logger.log('Running command for %s' % fNames)
>     try:
>
>         self.child = pexpect.spawn("scp %s %s@%s:%s"%(fNames, user, host,
> path))
>        # The script times out here:

Add a print here to see what is actually send to scp (or log it to your logger).

>         i = self.child.expect(['assword:', r"yes/no"], timeout=30)
>     except:
>         self.logger.logException()
>
>     if i==0:
>         self.logger.log('Sending password')
>         self.child.sendline(password)
>     elif i==1:
>         self.logger.log('Sending yes and password')
>         self.child.sendline("yes")
>         self.child.expect("assword:", timeout=30)
>         self.child.sendline(password)
>         try:
>             data = self.child.read()
>             self.logger.log(`data`)
>         except:
>             self.logger.logException()

The above 5 lines are only run when i == 1, not sure if this was intended.

>        self.child.expect(PROMPT)
>     self.logger.log('Done with SCP')

You never close the child so you *might*t have zombie processes
around. Which might cause the server not to respond to you. Regardless
It is always good to close so add self.child.close().

> This executes at the line " i = self.child.expect(['assword:', r"yes/no"],
> timeout=30)". From what I can see using tcpdump on the linux side, the scp
> traffic is going into the linux server, but it is not sending anything back.
> Is there anything obvious wrong here and is there a way I can see the exact
> command sent to out?
>
> The reason I chose to use pexpect is that is a pure Python method for doing
> interactive sessions for scp.
> Is there a different way of doing scp in a pure pythin self contained
> module? Piramiko is not an option because I cannot install it on the device
> I run my script on.

It works fine for me with the below function.

Br
Sander

def doScp(user,password, host, path, files):
    fNames = ' '.join(files)
    print fNames
    child = pexpect.spawn('scp %s %s@%s:%s' % (fNames, user, host,path))
    print 'scp %s %s@%s:%s' % (fNames, user, host,path)
        i = child.expect(['assword:', r"yes/no"], timeout=30)
        if i == 0:
            child.sendline(password)
        elif i == 1:
            child.sendline("yes")
            child.expect("assword:", timeout=30)
            child.sendline(password)
        data = child.read()
        print data
        child.close()
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to