Net:Telnet to Cisco Router

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

On Dec 14, 2007 6:27 PM, jackster the jackle [email protected]
wrote:

“Timeout” => 5,
taking my password for some reason and I know the password is good.
tn.waitfor(/Password: /)
tn.cmd(ENABLE) { |c| print c }
tn.cmd “Match” => /Password[: ]*\z/, “String” => “enable”
works just fine on our catalysts, I am not exactly sure how waitfor
and cmd interact, but the Match parameter should do the trick.

HTH
Robert

http://ruby-smalltalk.blogspot.com/


All truth passes through three stages. First, it is ridiculed. Second,
it is violently opposed. Third, it is accepted as being self-evident.
Schopenhauer (attr.)

You don’t have \n on the end of ENABLE, but I don’t know if that makes
a difference…

–Jeremy

On Dec 14, 2007 12:27 PM, jackster the jackle [email protected]
wrote:

“Timeout” => 5,
taking my password for some reason and I know the password is good.
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

Posted via http://www.ruby-forum.com/.


http://www.jeremymcanally.com/

My books:
Ruby in Practice

My free Ruby e-book

My blogs:

http://www.rubyinpractice.com/

On Dec 16, 2007 11:36 PM, jackster the jackle [email protected]
wrote:

** the problem I have now is with the tn.close command, the script times
out after the “show clock” command…any ideas?
yup, you are waiting for Username as a prompt now.
R.–

http://ruby-smalltalk.blogspot.com/


All truth passes through three stages. First, it is ridiculed. Second,
it is violently opposed. Third, it is accepted as being self-evident.
Schopenhauer (attr.)

thanks alot for the help from this forum…I was able to get into enable
mode and run a basic command with the following:

require ‘net/telnet’

CISCO = “172.30.152.1” #Enter the IP address here
USER = “username” #Enter username here
PASS = “password” #Enter password here
ENABLEPASS = “myenablepass”
SHOCLOCK = “show clock”

tn = Net::Telnet::new(“Host” => CISCO,
“Timeout” => 5,
“Prompt” => /^\Username:/ )

tn.cmd(“\n#{USER}”) { |c| print c }
tn.cmd(PASS) { |c| print c }
tn.print(“enable”) { |c| print c }
tn.print(“\n#{ENABLEPASS}”) { |c| print c }
tn.cmd(“\n#{SHOCLOCK}\n”) { |c| print c }
tn.close

** the problem I have now is with the tn.close command, the script times
out after the “show clock” command…any ideas?
thanks
jackster

Robert D. wrote:

On Dec 14, 2007 6:27 PM, jackster the jackle [email protected]
wrote:

“Timeout” => 5,
taking my password for some reason and I know the password is good.
tn.waitfor(/Password: /)
tn.cmd(ENABLE) { |c| print c }
tn.cmd “Match” => /Password[: ]*\z/, “String” => “enable”
works just fine on our catalysts, I am not exactly sure how waitfor
and cmd interact, but the Match parameter should do the trick.

HTH
Robert

http://ruby-smalltalk.blogspot.com/


All truth passes through three stages. First, it is ridiculed. Second,
it is violently opposed. Third, it is accepted as being self-evident.
Schopenhauer (attr.)

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)

Thanks Robert…

I have narrowed down my problem to this. All my logins, passwords and
commands are running. When I run “show clock” at the enable prompt, my
logs show that the command is being run:

< 0x00000: 73 68 6f 77 20 63 6c 6f 63 6b 0d 0a show
clock…

< 0x00000: 30 30 3a 33 37 3a 35 31 2e 31 37 33 20 45 53 54
00:37:51.173 EST
< 0x00010: 20 4d 6f 6e 20 44 65 63 20 31 37 20 32 30 30 37 Mon Dec
17 2007
< 0x00020: 0d 0a 43 54 31 2d 36 35 30 39 6f 6f 62 2d 30 31
…CT1-6509oob-01
< 0x00030: 23

My problem is that the command $obj->cmd(“show clock”) hangs and times
out but only after it runs the command (according to the Net::Telnet
debug_log) and according to my tacacs accounting log on the server.
How do I get this to stop hanging and continue on to the next command in
my script?

thanks

jackster

Robert D. wrote:

On Dec 16, 2007 11:36 PM, jackster the jackle [email protected]
wrote:

** the problem I have now is with the tn.close command, the script times
out after the “show clock” command…any ideas?
yup, you are waiting for Username as a prompt now.
R.–

http://ruby-smalltalk.blogspot.com/


All truth passes through three stages. First, it is ridiculed. Second,
it is violently opposed. Third, it is accepted as being self-evident.
Schopenhauer (attr.)