title: Python SSH via Pseudo TTY Clear
body: <p>I wrote the following python module to handle ssh connections in my program:</p>
  
  <pre><code>#!/usr/bin/env python
  from vxpty import VX_PTY
  
  class SSHError(Exception):
    def __init__(self, msg):
      self.msg = msg
    def __str__(self):
      return repr(self.msg)
  
  class SSHShell:
    def __init__(self, host, port, user, password):
      self.host = host
      self.port = port
      self.user = user
      self.password = password
      self.authenticated = False
    def authenticate(self):
      self.tty = VX_PTY(['/usr/bin/ssh', 'ssh', '-p'+str(self.port), self.user+'@'+self.host])
      resp = self.tty.read()
      if "authenticity of host" in resp:
        self.tty.println('yes')
        while 1:
          resp = self.tty.read()
          if "added" in resp:
            break
        resp = self.tty.read()
      if "assword:" in resp:
        self.tty.println(self.password)
        tmp_resp = self.tty.read()
        tmp_resp += self.tty.read()
        if "denied" in tmp_resp or "assword:" in tmp_resp:
          raise(SSHError("Authentication failed"))
        else:
          self.authenticated = True
          self.tty.println("PS1=''")
      return self.authenticated
    def execute(self, os_cmd):
      self.tty.println(os_cmd)
      resp_buf = self.tty.read().replace(os_cmd+'\r\n', '')
      return resp_buf
  </code></pre>
  
  <p>Which uses a pty module I wrote earlier:</p>
  
  <pre><code>#!/usr/bin/env python
  import os,pty
  
  class PTYError(Exception):
    def __init__(self, msg):
      self.msg = msg
    def __str__(self):
      return repr(self.msg)
  
  class VX_PTY:
    def __init__(self, execlp_args):
      self.execlp_args = execlp_args
      self.pty_execlp(execlp_args)
    def pty_execlp(self, execlp_args):
      (self.pid, self.f) = pty.fork()
      if self.pid==0:
        os.execlp(*execlp_args)
      elif self.pid&lt;0:
        raise(PTYError("Failed to fork pty"))
    def read(self):
      data = None
      try:
        data = os.read(self.f, 1024)
      except Exception:
        raise(PTYError("Read failed"))
      return data
    def write(self, data):
      try:
        os.write(self.f, data)
      except Exception:
        raise(PTYError("Write failed"))
    def fsync(self):
      os.fsync(self.f)
    def seek_end(self):
      os.lseek(self.f, os.SEEK_END, os.SEEK_CUR)
    def println(self, ln):
      self.write(ln+'\n')
  </code></pre>
  
  <p>However, whenever I call the <code>execute()</code> method, I end up reading the output from the first line:</p>
  
  <pre><code>&gt;&gt;&gt; import SSH;shell=SSH.SSHShell('localhost',22,'735tesla','notmypassword');shell.authenticate()
  True
  &gt;&gt;&gt; shell.execute('whoami')
  "\x1b[?1034hLaptop:~ 735Tesla$ PS1=''\r\n"
  &gt;&gt;&gt;
  </code></pre>
  
  <p>Then the second time I call <code>read()</code> I get the output:</p>
  
  <pre><code>&gt;&gt;&gt; shell.tty.read()
  'whoami\r\n735Tesla\r\n'
  &gt;&gt;&gt; 
  </code></pre>
  
  <p>Removing <code>whoami\r\n</code> from the output is not problem but is there any way to clear the output so I don't have to call read twice with the first command?</p>