[Tutor] Using pexpect to SCP files

Johan Geldenhuys johan at accesstel.com.au
Tue Jul 19 12:57:29 CEST 2011


Hi there all,

 

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

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

                

            self.child.expect(PROMPT)

        self.logger.log('Done with SCP')

 

 

This executes to the line in red and then times out. 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.

 

Thank for helping.

 

Johan

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110719/4f8f3156/attachment.html>


More information about the Tutor mailing list