Net:Telnet to Cisco Router

  • Thread starter jackster the jackle
  • Start date
J

jackster the jackle

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
 
J

Jeremy McAnally

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

--Jeremy

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



--
http://www.jeremymcanally.com/

My books:
Ruby in Practice
http://www.manning.com/mcanally/

My free Ruby e-book
http://www.humblelittlerubybook.com/

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/
 
R

Robert Dober

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.
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
 
J

jackster the jackle

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 said:
"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
 
J

jackster the jackle

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top