Forum: Ruby net/ssh for enable mode on cisco router

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jackster T. (Guest)
on 2008-11-22 16:35
I am having trouble getting into enable mode and running commands on a
cisco router using ssh. The following code allows me to successfully
login and run a command and display the output:
-----------------------------------
require "rubygems"
require "net/ssh"

Net::SSH.start( '10.1.1.1', 'jackster', :password => "my_password" ) do
|ssh_connection|

ssh_connection.open_channel do |channel|
 channel.on_data do |ch, data|
    puts data
end
channel.exec "sh arp" do |ch, success|

    if success
      puts "command has begun executing..."
    else
      puts "alas! the command could not be invoked!"
    end
end
end
ssh_connection.loop
end
-------------------------------
Here is my attempt at running the same command from after trying to get
into enable mode:

Net::SSH.start( '10.1.1.1', 'jackster', :password => "my_password" ) do
|ssh_connection|

ssh_connection.open_channel do |channel|
 channel.on_data do |ch, data|
    puts data
end
channel.exec "enable"
# I think I need something here to expect the "Password: " prompt before
sending
# the password?
channel.send_data("my_enable_password\n")
channel.exec "sh arp" do |ch, success|

    if success
      puts "command has begun executing..."
    else
      puts "alas! the command could not be invoked!"
    end
end
end
ssh_connection.loop
end
-------------------
The problem with this code is the script gets stuck on the "Password:"
prompt and waits for the enable password.

Any help getting past this would be appreciated.
thanks

Jackster
Brian C. (Guest)
on 2008-11-23 12:55
Not a direct answer, but I found the Net::SSH API too cumbersome to use
and so I wrote Net::SSH::Telnet, which was in turn tidied up and
released by Matthew Kent.

This gives you an API which is more-or-less drop-in compatible with
Net::Telnet.

http://rubyforge.org/projects/net-ssh-telnet/
Jackster T. (Guest)
on 2008-11-23 20:25
Thanks alot Brian.

I went to the link you posted and I am definately interested in the
code.
Do you have any code examples you can post?

thanks

jackster



Brian C. wrote:
> Not a direct answer, but I found the Net::SSH API too cumbersome to use
> and so I wrote Net::SSH::Telnet, which was in turn tidied up and
> released by Matthew Kent.
>
> This gives you an API which is more-or-less drop-in compatible with
> Net::Telnet.
>
> http://rubyforge.org/projects/net-ssh-telnet/
Brian C. (Guest)
on 2008-11-24 18:39
jackster the jackle wrote:
> Do you have any code examples you can post?

Inside the git repo is an examples/ directory - but perhaps this didn't
make it into the gem.

In any case you can find them via the rubyforge site. Follow the SCM tab
then the gitweb link. This takes you to

http://net-ssh-telnet.rubyforge.org/git?p=net-ssh-...

I suggest you start with get_hostname.rb

Once you've established the ssh connection, the API is the same as
Net::Telnet, so you can look at the Ruby API pages, or look for a file
like /usr/lib/ruby/1.8/net/telnet.rb on your system.

HTH,

Brian.
Jackster T. (Guest)
on 2008-11-25 02:59
Thanks Brian,

I am trying to run the following code after successfully installing the
gem but ruby can't seem to find net/telnet/ssh:

$: << File.dirname(__FILE__) + "/../lib"
require 'net/ssh/telnet'

s = Net::SSH::Telnet.new("Dump_log" => "/dev/stdout",
         "Host" => "10.1.1.1",
         "Username" => "jackster",
         "Password" => "password"
        )
 puts "Logged in"
 puts s.cmd("sh tac")

s.close

Here's the error message:

./netssh.rb:4:in `require': no such file to load -- net/ssh/telnet
(LoadError)
        from ./netssh.rb:4

Not sure if I'm missing something from installing the gem?
thanks
jackster
Brian C. (Guest)
on 2008-11-25 10:24
jackster the jackle wrote:
> Thanks Brian,
>
> I am trying to run the following code after successfully installing the
> gem but ruby can't seem to find net/telnet/ssh:
>
> $: << File.dirname(__FILE__) + "/../lib"

Replace this line with:

require 'rubygems'
This topic is locked and can not be replied to.