# Re: Telnet client

**URL:** <https://www.ruby-forum.com/t/re-telnet-client/148038>\
**Category:** Ruby\
**Created:** [September 8, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/re-telnet-client/148038 "2008-09-08T15:49:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [September 8, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/re-telnet-client/148038/1 "2008-09-08T15:49:03Z")

</div>

> end  
> end
> 
> It simply get what user input and put it on the telnet server.

I am having to guess here, but I imagine that tn is an instance of a  
Net::Telnet object?

You haven’t described exactly what your problem is. In what way do your  
“failed operations” fail? What exactly do the users type, what do they  
see  
in response, and what do you expect them to see?

I can see a few problems with the above code though.

- ‘gets’ receives a whole line at a time. If you try to use an  
interactive  
program like vi, it will fail. If that’s not what you want, have a  
look  
at Curses, HighLine, or ruby-termios.  
[codeidol.com](http://codeidol.com/other/rubyckbk/User-Interface/Getting-Input-One-Character-at-a-Time/)

- In the third line above, under ruby 1.8 the block parameter ‘c’ is  
bound  
to the same variable as the ‘c’ outside. This means that the value of  
‘c’  
will have changed after the block has executed. This means that if c  
was equal to ‘exit’ before, it may not be equal afterwards if the  
block  
has been invoked one or more times.

- c will never equal “exit” because you are reading whole lines with  
their  
terminator. It will be “exit\n”.

So a cleaned-up version of your code might be:

loop do  
c = gets  
tn.cmd(c) { |resp| print resp }  
break if c == “exit\n”  
end

Having said that, it would be better to detect if the far end drops the  
connection rather than seeing if the user typed “exit”. The user could  
legitimately type “exit” for reasons other than logging out; and they  
could  
logout by typing “logout”.

I’m not sure the best way to do that with Net::Telnet. If you’re waiting  
for  
a specific response then you can test whether the value returned from  
‘cmd’  
matches the string you were expecting. Otherwise, you can try sending  
data  
and rescuing the Errno::EPIPE you get if the other end has closed.

I’d say that Net::Telnet is really designed for scripting telnet  
sessions,  
not for writing your own interactive telnet client.

HTH,

Brian.

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [September 8, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/re-telnet-client/148038/2 "2008-09-08T15:49:04Z")

</div>

> ```
> c = gets
> 
> ```

One more point: that should say “c = STDIN.gets” for an interactive  
program.

“c = gets” will work until you try to pass arguments on the command line  
(using ARGV). That’s because the bare ‘gets’ will treat those arguments  
as  
the names of files to read.

Regards,

Brian.

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [September 9, 2008, 2:37pm UTC](https://www.ruby-forum.com/t/re-telnet-client/148038/3 "2008-09-09T14:37:18Z")

</div>

> Can anybody help me on this? Is there a problem with “gets”? I changed  
> it to “STDIN.gets” but still have such problem.

Hmm, I think someone said the gateway to ruby-forum was broken - is that  
still true?

My responses to this one are visible at  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/314220](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/314220)  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/314221](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/314221)

but not at  
[http://www.ruby-forum.com/topic/165048](http://www.ruby-forum.com/topic/165048)

Regards,

Brian.
