# Net::telnet

**URL:** <https://www.ruby-forum.com/t/net-telnet/91762>\
**Category:** Ruby\
**Created:** [April 12, 2007, 3:38am UTC](https://www.ruby-forum.com/t/net-telnet/91762 "2007-04-12T03:38:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![robtotheb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@robtotheb](https://www.ruby-forum.com/u/robtotheb)\
**Post date:** [April 12, 2007, 3:38am UTC](https://www.ruby-forum.com/t/net-telnet/91762/1 "2007-04-12T03:38:08Z")

</div>

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 B. 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 [Robs Home Page](http://www.cs.auckland.ac.nz/~rob)  
Private bag 92019 [http://www.wikarekare.org](http://www.wikarekare.org)  
Auckland 1001 [http://www.burrowes.org](http://www.burrowes.org)
