Forum: Ruby Net::telnet

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
2c562374d9df94b5528a33eca627778f?d=identicon&s=25 Rob Burrowes (Guest)
on 2007-04-12 03:38
(Received via mailing list)
I just wrote a small utility to pull Mac addresses verses port
numbers from our switches and found that the telnet.cmd strings
yielded to the block, were out of synch with the commands. It took me
a while to track it down to the telnet.cmd method of net/telnet.rb
adding a "\n" to the command, The network switch was expecting just a
single key stroke, which is common in text based menu driven
interfaces. The result being that the "\n" caused the switch to
resend to last menu, and I was getting two prompts for command I
sent. Hence the wait method got a command out of synch, for every
command I executed.

I fixed this with a hack to the net/telnet.rb library, which may be
useful to others.
This is the whole rewritten cmd method.

     def cmd(options) # :yield: recvdata
       match    = @options["Prompt"]
       time_out = @options["Timeout"]

       if options.kind_of?(Hash)
         string   = options["String"]
         match    = options["Match"]   if options.has_key?("Match")
         time_out = options["Timeout"] if options.has_key?("Timeout")
         no_nl = options["NoNL"] if options.has_key?("NoNL")
############# >>>>>>>>>>>ADDED THIS LINE
         string = options

       ########### >>>>>>>>>>>REPLACED self.puts(string) with this
"if" statement.
       if no_nl

       if block_given?
         waitfor({"Prompt" => match, "Timeout" => time_out}){|c|
yield c }
         waitfor({"Prompt" => match, "Timeout" => time_out})


Rob Burrowes                     Ph:   +64 (9) 3737 599 Extn 87972
Computing Services Manager        Fax: +64 (9) 3737 453
Computer Science Department    Mob:+64 (27) 4731 856
The University of Auckland
Rm 391, Level 3
38 Princes Street      
Private bag 92019      
Auckland 1001          
This topic is locked and can not be replied to.