title: Sending a password over SSH or SCP with subprocess.Popen
body: <p>I'm trying to run an <a href="http://en.wikipedia.org/wiki/Secure_copy" rel="noreferrer"><strong><code>scp</code></strong></a> (secure copy) command using <code>subprocess.Popen</code>.  The login requires that I send a password:</p>
  
  <pre><code>from subprocess import Popen, PIPE
  
  proc = Popen(['scp', "user@10.0.1.12:/foo/bar/somefile.txt", "."], stdin = PIPE)
  proc.stdin.write(b'mypassword')
  proc.stdin.flush()
  </code></pre>
  
  <p>This immediately returns an error:</p>
  
  <pre><code>user@10.0.1.12's password:
  Permission denied, please try again.
  </code></pre>
  
  <p>I'm <em>certain</em> the password is correct.  I easily verify it by manually invoking <code>scp</code> on the shell.  So why doesn't this work?</p>
  
  <p>Note, there are many similar questions to this, asking about <code>subprocess.Popen</code> and sending a password for automated SSH or FTP login:</p>
  
  <p><a href="https://stackoverflow.com/questions/4688441/how-can-i-set-a-users-password-in-linux-from-a-python-script">How can I set a users password in linux from a python script?</a><br>
  <a href="https://stackoverflow.com/questions/2387731/use-subprocess-to-send-a-password">Use subprocess to send a password</a></p>
  
  <p>The answer(s) to these questions don't work and/or don't apply because I am using Python 3.</p>