[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Net:Telnet to Cisco Router

jackster the jackle

12/14/2007 5:27:00 PM

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
--
Posted via http://www.ruby-....

5 Answers

Jeremy McAnally

12/14/2007 8:50:00 PM

0

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 <contact@thirdorder.net> 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
> --
> Posted via http://www.ruby-....
>
>



--
http://www.jeremymca...

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

My free Ruby e-book
http://www.humblelittlerub...

My blogs:
http://www.mrneigh...
http://www.rubyinpra...

Robert Dober

12/14/2007 9:38:00 PM

0

On Dec 14, 2007 6:27 PM, jackster the jackle <contact@thirdorder.net> 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.
> 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.
<snip>
>
HTH
Robert



--

http://ruby-smalltalk.blo...

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

jackster the jackle

12/16/2007 10:37:00 PM

0

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 Dober wrote:
> On Dec 14, 2007 6:27 PM, jackster the jackle <contact@thirdorder.net>
> 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.
> <snip>
>>
> HTH
> Robert
>
>
>
> --
>
> http://ruby-smalltalk.blo...
>
> ---
> 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.)

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

Robert Dober

12/17/2007 5:05:00 AM

0

On Dec 16, 2007 11:36 PM, jackster the jackle <contact@thirdorder.net> 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.blo...

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

jackster the jackle

12/17/2007 5:46:00 AM

0

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 Dober wrote:
> On Dec 16, 2007 11:36 PM, jackster the jackle <contact@thirdorder.net>
> 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.blo...
>
> ---
> 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.)

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