From: Martin Boese Date: 2007-12-17T15:09:43+09:00 Subject: Re: Net:Telnet to Cisco Router --Boundary-00=_LLhZHIdFbrAZO6R Content-Type: Multipart/Mixed; boundary="Boundary-00=_LLhZHIdFbrAZO6R" --Boundary-00=_LLhZHIdFbrAZO6R Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline I once wrote this attached class to run cisco commands and fetch configs. You can run any command with the cmd method, like this will fetch and print a running config of a device: require 'ciscotelnet' c = CiscoTelnet.new("Host" => '10.253.1.3', "Password" => "XXXX", "Enable" => "yyyyyy", "User" => "martin") c.open c.login c.enable c.cmd "terminal length 0" puts c.cmd("show run", 20) On Friday 14 December 2007 18:27:00 jackster the jackle wrote: > Hi Ruby Forum, > > I have some code that will login to my cisco router and run a basic > command. > > The problem is, I need to get into enable mode to run additional > command. Getting into enable mode requires typing "enable", waiting for > the prompt "Password: " then entering another password. > > This code works and gets me into the router but not in enable mode: > > #!/usr/local/bin/ruby > > require 'net/telnet' > > CISCO = "172.31.1.1" #Enter the IP address here > USER = "jsmith" #Enter username here > PASS = "mypassword" #Enter password here > ENABLE = "myenablepass" #Enter enable password here > > tn = Net::Telnet::new("Host" => CISCO, > "Timeout" => 5, > "Prompt" => /Username/ ) > > tn.cmd("\n#{USER}") { |c| print c } > tn.cmd(PASS) { |c| print c } > tn.cmd("sh mod\n") { |c| print c } > > Instead of sending the command "sh mod\n" as shown above, I want to get > into enable mode and send the command "sh run\n". > > The problem is, I get to the password prompt but the router is not > taking my password for some reason and I know the password is good. > > Here is the added code that I'm having trouble with: > > tn = Net::Telnet::new("Host" => CISCO, > "Timeout" => 5, > "Prompt" => /Username: / ) > > tn.cmd("\n#{USER}") { |c| print c } > tn.cmd(PASS) { |c| print c } > tn.cmd("enable\n") { |c| print c } > tn.waitfor(/Password: /) > tn.cmd(ENABLE) { |c| print c } > tn.cmd("sh run\n") { |c| print c } > > Any ideas to get me into enable mode here would be greatly appreciated. > thanks > jackster --Boundary-00=_LLhZHIdFbrAZO6R 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=_LLhZHIdFbrAZO6R-- --Boundary-00=_LLhZHIdFbrAZO6R--