[Tutor] Resend: Using pexpect to SCP files

Johan Geldenhuys johan at accesstel.com.au
Tue Jul 19 13:20:34 CEST 2011


Resend in text format

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))
        # The script times out here: 
        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 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.

Thank for helping.

Johan




More information about the Tutor mailing list