title: Ruby Net::SSH get Exit Status in PTY
body: <p>I'm trying to write in some functionality into the Net::SSH::Telnet Library for a project of mine.  Specifically I need to be able to read the exit-status of any command that I write.</p>
  
  <p>Here's the Original source from the Net::SSH::Telnet Library that I'm modifying</p>
  
  <pre><code>@buf = ""
  @eof = false
  @channel = nil
  @ssh.open_channel do |channel|
    channel.request_pty { |ch,success|
      if success == false
        raise "Failed to open ssh pty"
      end
    }
    channel.send_channel_request("shell") { |ch, success|
      if success
        @channel = ch
        waitfor(@options['Prompt'], &amp;blk)
        return
      else
        raise "Failed to open ssh shell"
      end
    }
    channel.on_data { |ch,data| @buf &lt;&lt; data }
    channel.on_extended_data { |ch,type,data| @buf &lt;&lt; data if type == 1 }
    channel.on_close { @eof = true }
  end
  @ssh.loop
  </code></pre>
  
  <p>And now here are the changes I have made:</p>
  
  <pre><code>@stderr = ""
  @exit_code = nil
  @buf = ""
  @eof = false
  @channel = nil
  @ssh.open_channel do |channel|
    channel.request_pty { |ch,success|
      if success == false
        raise "Failed to open ssh pty"
      end
    }
    channel.send_channel_request("shell") { |ch, success|
      if success
        @channel = ch
        waitfor(@options['Prompt'], &amp;blk)
        return
      else
        raise "Failed to open ssh shell"
      end
    }
    channel.on_data { |ch,data| 
      @stdout &lt;&lt; data
      @buf &lt;&lt; data
    }
    channel.on_extended_data { |ch,type,data| 
      @stderr &lt;&lt; data
      @buf &lt;&lt; data if type == 1
    }
    channel.on_request("exit-status") do |ch,data|
      @exit_code = data.read_long
    end
    channel.on_close { 
      @eof = true
    }
  end
  @ssh.loop
  </code></pre>
  
  <p>The problem is that when I run this, the @exit_code variable never changes from nil.</p>
  
  <p>The reason I am doing this is because I need three major pieces of functionality: 
  1. Stateful (interactive) communication (so that if I cd into a directory the next command will occur in that directory)
  2. The ability to read the exit status of any command I execute
  3. The ability to respond to prompts</p>
  
  <p>I selected Net::SSH::Telnet to adjust because it already offered 1 &amp; 3.  I figured I could fix it to support number 2.<br>
  If anyone can see how to fix this I would greatly appreciate it.  The source for the rest of Net::SSH::Telnet is here:</p>
  
  <p><a href="https://github.com/jasonkarns/net-ssh-telnet/" rel="nofollow">https://github.com/jasonkarns/net-ssh-telnet/</a></p>
  
  <p>and I have a forked version of the repo here to which I would gladly accept pull requests.  </p>
  
  <p><a href="https://github.com/0x783czar/net-ssh-remotescript" rel="nofollow">https://github.com/0x783czar/net-ssh-remotescript</a></p>
  
  <p>Also if anyone has a suggestion of any other ruby libraries that would offer those three functionalities I would greatly appreciate any suggestions.</p>