Re: Telnet client


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
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
    program like vi, it will fail. If that’s not what you want, have a
    at Curses, HighLine, or ruby-termios.

  • In the third line above, under ruby 1.8 the block parameter ‘c’ is
    to the same variable as the ‘c’ outside. This means that the value of
    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
    has been invoked one or more times.

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

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

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

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
logout by typing “logout”.

I’m not sure the best way to do that with Net::Telnet. If you’re waiting
a specific response then you can test whether the value returned from
matches the string you were expecting. Otherwise, you can try sending
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
not for writing your own interactive telnet client.



c = gets

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

“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
the names of files to read.



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

but not at



This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs