Net::telnet

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
       else
         string = options
       end

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

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

Rob

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 http://www.cs.auckland.ac.nz/~rob
Private bag 92019 http://www.wikarekare.org
Auckland 1001 http://www.burrowes.org