net/ssh for enable mode on cisco router

  • Thread starter jackster the jackle
  • Start date
J

jackster the jackle

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
 
J

jackster the jackle

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
 
B

Brian Candler

jackster said:
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...0ed1db1c8bb18d6fb08cfe1a439191a2e9cbb;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.
 
J

jackster the jackle

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
 
B

Brian Candler

jackster said:
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'
 

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

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top