title: scp to a remote server using pexpect
body: <p>I'm trying to learn a little bit on pexpect: in particular I'm trying to copy a file from my laptop to a remote server.
  I'm experiencing a weird behaviour: more or less the same code works if I write it line by line but it won't if I run it as a script.
  Here is what I write line-by-line:</p>
  
  <pre><code>child = pexpect.spawn('scp pathdir/file.ext username@hostname:pathdir')
  r=child.expect ('assword:')
  r
  </code></pre>
  
  <p>it returns 0 and I finish the job with the password</p>
  
  <pre><code>child.sendline ('password')
  </code></pre>
  
  <p>When I do ssh to the server I found my file there. So I collect all the steps in a script; it exits without errors, but the file it was not copied... why? But more importantly, how can I fix that?</p>
  
  <p>Here is the script:</p>
  
  <pre><code>child = pexpect.spawn('scp pathdir/file.ext username@hostname:pathdir')
  r=child.expect ('assword:')
  print r
  if r==0:
      child.sendline ('password')
  child.close()
  </code></pre>
  
  <p>I'm not sure how pexpect works so I print r to be sure it is 0. And it is. </p>