title: How to pass command in command to pexpext in python
body: <p>I need in python execute this command and enter password from keyboard, this is works:</p>
  
  <pre><code>import os
  cmd = "cat /home/user1/.ssh/id_rsa.pub | ssh user2@host.net \'cat &gt;&gt; .ssh/authorized_keys\' &gt; /dev/null 2&gt;&amp;1"
  os.system(cmd)
  </code></pre>
  
  <p>As you can see I want append public key to remote host via ssh.<br/>
  See here: <a href="https://stackoverflow.com/questions/9971490/equivalent-of-ftp-put-and-append-in-scp">equivalent-of-ftp-put-and-append-in-scp</a> and here: <a href="https://stackoverflow.com/questions/13650312/copy-and-append-files-to-a-remote-machine-cat-error">copy-and-append-files-to-a-remote-machine-cat-error</a></p>
  
  <p>Of course I want it do it without user input I've try pexpect and I think command is to weird for it:</p>
  
  <pre><code>import pexpect
  
  child = pexpect.spawn(command=cmd, timeout=10, logfile=open('debug.txt', 'a+'))
  matched = child.expect(['Password:', pexpect.EOF, pexpect.TIMEOUT])
  if matched == 0:
      child.sendline(passwd)
  </code></pre>
  
  <p>in debug.txt:</p>
  
  <pre><code>ssh-rsa AAAA..........vcxv233x5v3543sfsfvsv user1@host1
  /bin/cat: |: No such file or directory
  /bin/cat: ssh: No such file or directory
  /bin/cat: user2@host.net: No such file or directory
  /bin/cat: cat &gt;&gt; .ssh/authorized_keys: No such file or directory
  /bin/cat: &gt;: No such file or directory
  /bin/cat: 2&gt;&amp;1: No such file or directory
  </code></pre>
  
  <p>I see two solution:</p>
  
  <ol>
  <li>fix command for pexpect, that it recognize whole string as one command or,</li>
  <li>inject/write passwd  to stdin as fake user, but how!?!?</li>
  </ol>