views:

62

answers:

1

Hello,

I am using ssh to log into a camera, scp a tarball over to it and extract files from the tarbal and then run the script. I am having problems with Pexpect, though. Pexpect times out when the tarball is being copied over. It seem's not to wait until it is done. And then it start's doing the same thing with the untar command, The code I have is below:

    ssh_newkey = 'Are you sure you want to continue connecting'          
    copy = pexpect.spawn('ssh [email protected]')
    i=copy.expect([ssh_newkey,'password:',pexpect.EOF])
    if i==0:
        copy.sendline('yes')
        i=copy.expect([ssh_newkey,'password:',pexpect.EOF])
    if i==1:        
        copy.sendline("service")
        print 'Password Accepted'
        copy.expect('service@user:')
        copy.sendline('su - root')
        i=copy.expect('Password:')
        copy.sendline('root')
        i=copy.expect('#')
        copy.sendline('cd /tmp')
        i=copy.expect("#")
        copy.sendline('scp [email protected]:/home/user/tarfile.tar.gz .')
        i=copy.expect([ssh_newkey,'password:',pexpect.EOF])
        if i==0:
            copy.sendline('yes')
            i=copy.expect([ssh_newkey,'password:',pexpect.EOF])
        else:
            pass
        copy.sendline('userpwd')
        i=copy.expect('#')
        copy.sendline('tar -zxvf tarfile.tar.gz bin/installer.sh')
        i=copy.expect("#")
        copy.sendline("setsid /tmp/bin/installer.sh /tmp/tarfile.tar.gz > /dev/null 2>&1 &")            
    elif i==2:
        print "I either got key or connection timeout"
    else:
        pass

Can anyone help find a solution for this?

Thanks

+1  A: 

I'm not sure if this is correct, but I'd try setting the timeout to None:

copy = pexpect.spawn('ssh [email protected]', timeout=None)

According to the source code, pexpect seems to not check the timeout when it's set to None.

Anyway, the reason I'm answering this even though I'm not sure whether it solves your problem is that I wanted to recommend using paramiko instead. I had good experience using it for communication over SSH in the past.

Lukáš Lalinský
@Lukáš -- I feel a bit silly now, I never thought of that. The timeout is set to 30 seconds and the tarbal takes longer than that to copy over. This work's now. Thanks +1
chrissygormley