# Trying to Use "request\_pty" Channel Never Closes

**URL:** <https://www.ruby-forum.com/t/trying-to-use-request-pty-channel-never-closes/152511>\
**Category:** Ruby\
**Created:** [November 4, 2008, 7:31pm UTC](https://www.ruby-forum.com/t/trying-to-use-request-pty-channel-never-closes/152511 "2008-11-04T19:31:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![scoobycoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a8b319/32.png) [@scoobycoder](https://www.ruby-forum.com/u/scoobycoder)\
**Post date:** [November 4, 2008, 7:31pm UTC](https://www.ruby-forum.com/t/trying-to-use-request-pty-channel-never-closes/152511/1 "2008-11-04T19:31:12Z")

</div>

I’m trying to interact with a RF Terminal application. But each time I  
run this, it returns nothing from stdout, and just hangs irb. I’ve been  
banging my head on this one for a couple days, because it seems right,  
but nothing comes back from the server.

Any ideas? Thanks in advance. 🙂

```auto
  def test_rf(run_this)

    Net::SSH.start('xx.xx.xx.xx', 'userid', :password => "password") do
|session|

      session.open_channel do |channel|
        channel.request_pty(:term => 'xterm') do |ch, success|
          raise "could not request pty!" unless success
          channel.send_data "2\n" # tell the shell to exit
        end
        puts "shell was started successfully!"
        channel.send_data "2\n" # tell the shell to exit
        sleep 3
        channel.send_data "1\n" # tell the shell to exit
        sleep 2
        channel.send_data "Canada6-gen\t"
        channel.send_data "Canada6-gen\t"
        channel.send_data "\n"
        channel.send_data "<ESC>0"
        channel.send_data "2\n"
        channel.send_data "0\n"

        channel.on_data do |ch, data|
          puts "got data: #{data.inspect}"
        end
        channel.on_close do
          puts "shell terminated"
        end

      end

      session.loop

    end

  end #test_rf
```

---

<div class="post-metadata">

**Author:** ![scoobycoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a8b319/32.png) [@scoobycoder](https://www.ruby-forum.com/u/scoobycoder)\
**Post date:** [November 4, 2008, 7:32pm UTC](https://www.ruby-forum.com/t/trying-to-use-request-pty-channel-never-closes/152511/2 "2008-11-04T19:32:51Z")

</div>

Oh, and ignore the comments.

---

<div class="post-metadata">

**Author:** ![scoobycoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a8b319/32.png) [@scoobycoder](https://www.ruby-forum.com/u/scoobycoder)\
**Post date:** [November 4, 2008, 8:06pm UTC](https://www.ruby-forum.com/t/trying-to-use-request-pty-channel-never-closes/152511/3 "2008-11-04T20:06:34Z")

</div>

On Wed, 5 Nov 2008, Nathan Halterman wrote:

> I’m trying to interact with a RF Terminal application. But each time I  
> run this, it returns nothing from stdout, and just hangs irb. I’ve been  
> banging my head on this one for a couple days, because it seems right,  
> but nothing comes back from the server.
> 
> Any ideas? Thanks in advance. 🙂

While you’re waiting for someone who knows Net::ssh to reply, I’d add  
LOTS more diagnostics:

> [code]  
> def test\_rf(run\_this)
> 
> ```
> Net::SSH.start('xx.xx.xx.xx', 'userid', :password => "password") do
> 
> ```
> 
> |session|
> 
> ```
> session.open_channel do |channel|
> channel.request_pty(:term => 'xterm') do |ch, success|
> raise "could not request pty!" unless success
> 
> ```

- 

```
     puts "about to send 2\\n"
     # channel.send_data "2\n" # tell the shell to exit
     x = channel.send_data "2\n" # tell the shell to exit
     puts "sent, the result was #{x}"

```

> ```
> channel.send_data "2\n"
> channel.send_data "0\n"
> 
> channel.on_data do |ch, data|
> puts "got data: #{data.inspect}"
> end
> channel.on_close do
> puts "shell terminated"
> end
> 
> end
> 
> ```

```
    puts "session.open_channel completed, about to call loop"

```

> ```
> session.loop
> 
> ```

```
    puts "got past loop statement"

```

> ```
> end
> 
> ```

```
  puts "Net ssh start completed.

```

---

<div class="post-metadata">

**Author:** ![scoobycoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a8b319/32.png) [@scoobycoder](https://www.ruby-forum.com/u/scoobycoder)\
**Post date:** [November 4, 2008, 8:25pm UTC](https://www.ruby-forum.com/t/trying-to-use-request-pty-channel-never-closes/152511/4 "2008-11-04T20:25:29Z")

</div>

Thanks, that got me a little more information. The output was this.

[snip]

Net ssh start completed.  
session.open\_channel completed, about to call loop  
about to send 2\n  
sent, the result was 2

[/snip]

Hugh S. wrote:

> On Wed, 5 Nov 2008, Nathan Halterman wrote:
> 
> > I’m trying to interact with a RF Terminal application. But each time I  
> > run this, it returns nothing from stdout, and just hangs irb. I’ve been  
> > banging my head on this one for a couple days, because it seems right,  
> > but nothing comes back from the server.
> > 
> > Any ideas? Thanks in advance. 🙂
> 
> While you’re waiting for someone who knows Net::ssh to reply, I’d add  
> LOTS more diagnostics:
> 
> > [code]  
> > def test\_rf(run\_this)
> > 
> > ```
> > Net::SSH.start('xx.xx.xx.xx', 'userid', :password => "password") do
> > 
> > ```
> > 
> > |session|
> > 
> > ```
> > session.open_channel do |channel|
> > channel.request_pty(:term => 'xterm') do |ch, success|
> > raise "could not request pty!" unless success
> > 
> > ```
> 
> - 
> 
> ```
> puts "about to send 2\\n"
> # channel.send_data "2\n" # tell the shell to exit
> x = channel.send_data "2\n" # tell the shell to exit
> puts "sent, the result was #{x}"
> 
> ```
> 
> > ```
> > channel.send_data "2\n"
> > channel.send_data "0\n"
> > 
> > channel.on_data do |ch, data|
> > puts "got data: #{data.inspect}"
> > end
> > channel.on_close do
> > puts "shell terminated"
> > end
> > 
> > end
> > 
> > ```
> 
> ```
> puts "session.open_channel completed, about to call loop"
> 
> ```
> 
> > ```
> > session.loop
> > 
> > ```
> 
> ```
> puts "got past loop statement"
> 
> ```
> 
> > ```
> > end
> > 
> > ```
> 
> ```
> puts "Net ssh start completed.
> 
> ```

---

<div class="post-metadata">

**Author:** ![scoobycoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a8b319/32.png) [@scoobycoder](https://www.ruby-forum.com/u/scoobycoder)\
**Post date:** [November 4, 2008, 8:48pm UTC](https://www.ruby-forum.com/t/trying-to-use-request-pty-channel-never-closes/152511/5 "2008-11-04T20:48:16Z")

</div>

OOOOOkaaaaay. 🙂

So it turns out that it was a timing issue. I was sending the  
channel.on\_data too fast for the UI to process. I added in some sleep  
statements and it worked.

Thanks a ton! 🙂

---

<div class="post-metadata">

**Author:** ![scoobycoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a8b319/32.png) [@scoobycoder](https://www.ruby-forum.com/u/scoobycoder)\
**Post date:** [November 6, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/trying-to-use-request-pty-channel-never-closes/152511/6 "2008-11-06T15:59:27Z")

</div>

Thanks Hugh,

I’m not sure how to do an echo command with Ruby SSH while in a console  
application. The first thing I should see is a text based menu.

I am unable to get anything from stdout it seems. When I do the same  
SSH manually from outside of Ruby, I do get the menu to come up.  
Additionally when I just do a straight session.exec command to connect  
to the server and send something, what I send doesn’t matter, I can see  
the menu. This means to me that something is being sent back through  
stdout, but I can’t get it with the code below.

So this piece of code never seems to send me back anything.

```auto

        channel.on_data do |ch, data|
          puts "got data: #{data.inspect}"
        end

```

Thanks Again,

Nathan

Hugh S. wrote:

> On Wed, 5 Nov 2008, Nathan Halterman wrote:
> 
> > OOOOOkaaaaay. 🙂
> > 
> > So it turns out that it was a timing issue. I was sending the  
> > channel.on\_data too fast for the UI to process. I added in some sleep  
> > statements and it worked.
> 
> Pardon the spurious + in my previous example, I started to write it in  
> the style of a patch to make it clear what I had changed, then thought  
> that would actually make it more obscure!
> 
> I’m not familiar with Net::ssh, but I suspect there will be a way to  
> read  
> from the remote machine as well as write to it. I’d recommend, that if  
> you have echoing on (you can see what you type) that you try to read  
> back  
> what you sent. That way you will have less dependence on “magic” timing  
> numbers, though you will have to be careful how you match what you get  
> back,  
> as is the case when you use Expect. If the system gets loaded and you  
> have  
> to wait for the echo, then that’s still OK.
> 
> > ## Thanks a ton! 🙂
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> ```
> Hugh
> 
> ```

---

<div class="post-metadata">

**Author:** ![scoobycoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a8b319/32.png) [@scoobycoder](https://www.ruby-forum.com/u/scoobycoder)\
**Post date:** [November 4, 2008, 10:48pm UTC](https://www.ruby-forum.com/t/trying-to-use-request-pty-channel-never-closes/152511/7 "2008-11-04T22:48:30Z")

</div>

On Wed, 5 Nov 2008, Nathan Halterman wrote:

> OOOOOkaaaaay. 🙂
> 
> So it turns out that it was a timing issue. I was sending the  
> channel.on\_data too fast for the UI to process. I added in some sleep  
> statements and it worked.

Pardon the spurious + in my previous example, I started to write it in  
the style of a patch to make it clear what I had changed, then thought  
that would actually make it more obscure!

I’m not familiar with Net::ssh, but I suspect there will be a way to  
read  
from the remote machine as well as write to it. I’d recommend, that if  
you have echoing on (you can see what you type) that you try to read  
back  
what you sent. That way you will have less dependence on “magic” timing  
numbers, though you will have to be careful how you match what you get  
back,  
as is the case when you use Expect. If the system gets loaded and you  
have  
to wait for the echo, then that’s still OK.

> ## Thanks a ton! 🙂
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

```
    Hugh

```

---

<div class="post-metadata">

**Author:** ![scoobycoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a8b319/32.png) [@scoobycoder](https://www.ruby-forum.com/u/scoobycoder)\
**Post date:** [November 7, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/trying-to-use-request-pty-channel-never-closes/152511/8 "2008-11-07T15:44:49Z")

</div>

Last thing, the code above is 2.0 code. Here is the 1.0 code.

```auto

require 'rubygems'
require 'net/ssh'

Net::SSH.start('xx.xx.xx.xx', 'userid', "password") do |session|
  session.open_channel do |channel|
    channel.on_success do
      puts "pty was requested successfully!"
      channel.on_success do
        puts "shell was started successfully!"
        channel.send_data "2\n" # tell the shell to exit
                sleep 3
                channel.send_data "1\n" # tell the shell to exit
                sleep 2
                channel.send_data "login"
                channel.send_data "password"
                channel.send_data "\n"
                channel.send_data "<ESC>0"
                channel.send_data "2\n"
                channel.send_data "0\n"
      end

      channel.send_request "shell", nil, true
    end

    channel.on_failure do
      puts "shell could not be started!"
    end

    channel.on_data do |ch,data|
      puts "recieved #{data} from shell"
    end

    channel.on_close do
      puts "shell terminated"
    end

    channel.request_pty :want_reply => true

  end

  session.loop

end

```

---

<div class="post-metadata">

**Author:** ![scoobycoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a8b319/32.png) [@scoobycoder](https://www.ruby-forum.com/u/scoobycoder)\
**Post date:** [November 7, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/trying-to-use-request-pty-channel-never-closes/152511/9 "2008-11-07T15:43:06Z")

</div>

Okay, so I can’t stand when someone finally finds the issue, but doesn’t  
reply back to a forum.

Here is my solution. The problem was that SSH 2.0 did not work for my  
connection. I had to install SSH 1.0~ and then it worked for me.

Thanks for your help. 🙂

---

<div class="post-metadata">

**Author:** ![scoobycoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a8b319/32.png) [@scoobycoder](https://www.ruby-forum.com/u/scoobycoder)\
**Post date:** [November 8, 2008, 1:03am UTC](https://www.ruby-forum.com/t/trying-to-use-request-pty-channel-never-closes/152511/10 "2008-11-08T01:03:50Z")

</div>

On Thu, 6 Nov 2008, Nathan Halterman wrote:

> Thanks Hugh,
> 
> I’m not sure how to do an echo command with Ruby SSH while in a console  
> application. The first thing I should see is a text based menu.

I’m not talking about an echo command (as per the shell command echo).  
I’m  
talking about terminal settings, the sort of thing that stty handles  
under  
unix. If you press a key on the keyboard, does the local “terminal”  
echo  
it back to you (so you see it on the screen) or does the remote machine  
do  
that? If the remote machine does the echoing, as is normal nowadays in  
my  
experience, then you are best waiting for those chars you send to come  
back  
to you, before the response to the command.

> [code]
> 
> ```
> channel.on_data do |ch, data|
> puts "got data: #{data.inspect}"
> end
> 
> ```

OK, I’ve had a quick look at the docs to see what this is about.  
[http://net-ssh.rubyforge.org/ssh/v2/api/index.html](http://net-ssh.rubyforge.org/ssh/v2/api/index.html)  
This is a callback, but there are lots of callbacks to register.  
I think you’ll need callbacks for on\_data (like you have), on\_eof,  
on\_request (for which you need to specify a type, but I don’t know  
what they are, I can’t see any mention of which class they are, or  
what they respond\_to), on\_open\_channel, on\_extended\_data.

The system seems to be event based, and you need to trap as many of  
those as you can get your hands on to see what is happening.  
Apart from that, you need to look about for other people’s code that  
uses this, to see what “custom and practice” are, because I’m getting  
out of my depth.

I’d tend to simplify this and use Expect, and maybe invoke that from  
Ruby, but I’ve had experience of Expect, so your feelings about  
switching to Tcl for this bit could well be different with good reasons.

> [/code]
> 
> Thanks Again,
> 
> Nathan

```
    HTH
    Hugh

```
