Rajesh
Rajesh

Reputation: 75

pexpect not working properly

I am trying to copy some files from remote server to my machine. I use spawn in pexpect for authenticating when asked. I am partially successful in downloading the file from server. The Problem is before completing the download its throwing an exception " ETAException pexpect.ExceptionPexpect: ExceptionPexpect() in > ignored"

Below is my code:

def doScp(user,password,host,remotepath,localpath,files):
try:
    print files
    child = pexpect.spawn('sudo scp -C %s:%s%s %s' % (host, remotepath, files, localpath))
    child.logfile = sys.stdout
    print 'scp -C %s:%s%s %s' % (host, remotepath, files, localpath)
    i = child.expect(['assword', r"yes/no"], timeout=20)
    if i == 0:
        print "Value of I is Zero\n"
        child.sendline(password)
        j = child.expect(['yes/no'],timeout=20)
        if j == 0:
            child.sendline("yes")
        child.expect(pexpect.EOF, timeout=None)
    elif i == 1:
        child.sendline("yes")
        child.expect("assword", timeout=20)
        child.sendline(password)
        child.expect(pexpect.EOF, timeout=None)
    child.interact()
except pexpect.ExceptionPexpect, e:
    return False

Upvotes: 0

Views: 4247

Answers (1)

Rajesh
Rajesh

Reputation: 75

I Found the answer by myself itself. The issue is with the timeout. I have given none for the timeout now its working fine :) Here is my code

def doScp(user,password,host,remotepath,localpath,files):
    try:
        print files
        child = pexpect.spawn('sudo scp -C %s:%s%s %s' % (
                              host, remotepath, files, localpath))
        child.logfile = sys.stdout
        print 'scp -C %s:%s%s %s' % (host, remotepath, files, localpath)
        i = child.expect(['assword', r"yes/no"], timeout=None)
        if i == 0:
            print "Value of I is Zero\n"
            child.sendline(password)
            j = child.expect(['yes/no'],timeout=None)
            if j == 0:
                child.sendline("yes")
            child.expect(pexpect.EOF, timeout=None)
        elif i == 1:
            child.sendline("yes")
            child.expect("assword", timeout=None)
            child.sendline(password)
            child.expect(pexpect.EOF, timeout=None)
        child.interact()
    except pexpect.ExceptionPexpect, e:
        return False

Upvotes: 1

Related Questions