Net/ssh for enable mode on cisco router


#1

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


#2

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/


#3

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/


#4

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-telnet.git;a=tree;f=examples;h=5ca0ed1db1c8bb18d6fb08cfe1a439191a2e9cbb;hb=HEAD

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.


#5

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


#6

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’