From: Martin Boese Date: 2007-04-01T17:55:21+09:00 Subject: Re: Ruby & Telnet & Cisco --Boundary-00=_LN3DGl+5PS/LhCP Content-Type: Multipart/Mixed; boundary="Boundary-00=_LN3DGl+5PS/LhCP" --Boundary-00=_LN3DGl+5PS/LhCP Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline I don't know perl's cisco library, but this (see attached) is what I wrote to send commands to cisco routers and switches or fetch some values. To get it's running config for example I do this: ------------------------------------ require 'ciscotelnet' c = CiscoTelnet.new("Host" => '10.253.1.8', "Password" => "users-password", "Enable" => "enable-password", "User" => "martin") c.open c.login c.enable c.cmd "terminal length 0" puts c.cmd("show run", 20) ------------------------------------ Martin On Saturday 31 March 2007 13:40:15 Robert Dober wrote: > On 3/31/07, Brian Candler wrote: > > On Sat, Mar 31, 2007 at 12:48:22AM +0900, Pablo Zorzoli wrote: > > > Is there any approach similar to perl's Net:Telnet:Cisco [1] in ruby? > > > Has anyone nice experiences in this mix? > > > > Not that I'm aware of. But if you discover something please let me know - > > I'm going to have to do this myself in the next few weeks too. > > > > I was planning just to use plain Net::Telnet, which I think is good > > enough for what I need. > > Please kindly let me know about this, I was trying to do this with > Catalyst 29xx series Switches and a 2600 router, no luck so far, I > guess the prompt, timeout selection is quite tricky :( > > Cheers > Robert > > >I was also planning to try to make a wrapper for Net::SSH > > to make it provide an IO object that can be passed into Net::Telnet. > > > > Regards, > > > > Brian. --Boundary-00=_LN3DGl+5PS/LhCP Content-Type: application/x-ruby; name="ciscotelnet.rb" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="ciscotelnet.rb" require 'net/telnet' class CiscoTelnet require 'net/telnet' def initialize options @host = options["Host"] || "localhost" @user = options["User"] || "martin" @password = options["Password"] || "nopass" @enable = options["Enable"] || "nopass" @debug = options["Debug"] || false @prompt = nil end def debug_out(msg) $stdout.puts ">> " + msg if @debug end def debug_in(msg) $stdout.puts "<< " + msg if @debug end def open @socket = Net::Telnet.new("Host" => @host) end def close @socket.close end # returns last line received def expect(str) ret = @socket.waitfor("String" => str, "Timeout" => 10) { |rv| debug_in(rv) } return ret.split('\n').last.strip end # overwrite to match dynamic prompt def cmd(command, timeout=10) debug_out command return @socket.cmd("String" => command, "Match" => Regexp.new(Regexp.escape(@prompt)), "Timeout" => timeout) { |c| debug_in(c) } end def print(s) @socket.print(s) { |c| debug_out c } end def puts(s) @socket.puts(s) { |c| debug_out c } end def login expect "Username" puts @user expect "Password" puts @password @prompt = expect '>' $stdout.puts "prompt is " + @prompt if @debug end def enable puts "enable" expect "Password" puts @enable @prompt = expect '#' $stdout.puts "new prompt is " + @prompt if @debug end end --Boundary-00=_LN3DGl+5PS/LhCP-- --Boundary-00=_LN3DGl+5PS/LhCP--