title: How to prevent pexpect from echoing the password?
body: <p>By default, <code>pexpect.spawn()</code> would not output anything. But when I specify <code>logfile=sys.stdout</code> it'll also echo the password (e.g. for <code>ssh</code>). So how can I see the real time interaction with the <code>spawn</code>ed process without the password being echoed (just like the <code>Expect</code> (the <code>Tcl</code> extension) does)?</p>
  
  <p><strong>pexepct example:</strong></p>
  
  <pre class="lang-none prettyprint-override"><code># cat expect.py
  import pexpect, sys
  
  logfile = sys.stdout if len(sys.argv) == 2 else None
  
  ssh = pexpect.spawn('ssh foo@localhost', logfile=logfile)
  ssh.delaybeforesend = 1
  ssh.expect('assword:')
  ssh.sendline('123456')
  
  ssh.expect('\r\n\\$')
  ssh.sendline('exit')
  ssh.expect(pexpect.EOF)
  ssh.wait()
  # python expect.py                 &lt;-- no output
  # python expect.py stdout
  foo@localhost's password: 123456   &lt;-- the password is visible
  Last login: Tue Mar 22 10:32:49 2016 from localhost
  $ exit
  exit
  Connection to localhost closed.
  #
  </code></pre>
  
  <p><strong>Expect example:</strong></p>
  
  <pre class="lang-none prettyprint-override"><code># cat ssh.exp
  spawn ssh foo@localhost
  expect assword:
  send "123456\r"
  expect {\$}
  send "exit\r"
  expect eof
  wait
  # expect ssh.exp
  spawn ssh foo@localhost
  foo@localhost's password:          &lt;-- the password is invisible
  Last login: Tue Mar 22 10:45:03 2016 from localhost
  $ exit
  Connection to localhost closed.
  #
  </code></pre>