1

I'd like to be able to use the ruby net-ssh gem to make changes to a Juniper M10i router. However, after sending "configure", I am unable to send any configuration commands.

For example, after logging in to the router via ssh, I'd like to issue the following three commands:

configure
show system
exit

Using the net-ssh library, i've tried the following without success:

# net-ssh (2.3.0)
require 'net/ssh'

session = Net::SSH.start(host, user, :password => password)
session.exec!('term length 0')
session.exec!('term width 0')

channel = session.open_channel do |ch|
  puts "Channel open."

  ch.on_close do |ch|
    puts "Channel is closing!"
  end

  ch.on_data do |ch, data|
    puts "Data #{data}!!!!!!!!!!"
  end

  ch.exec "configure" do |ch, success|
    puts "FAIL: configure" unless success
  end

  ch.exec "show system" do |ch, success|
    puts "FAIL: show system" unless success
  end

  ch.exec "exit" do |ch, success|
    puts "FAIL: exit" unless success
  end
end
session.loop

Upon execution I get the following output:

Channel open.
FAIL: show system
FAIL: exit
Data Entering configuration mode
!!!!!!!!!!
Channel is closing!

So how do I correctly pass the "show system" command after "configure"?

SOLVED:

I stumbled across the following post: https://stackoverflow.com/a/6807178/152852

Community
  • 1
  • 1
uhleeka
  • 698
  • 8
  • 19

2 Answers2

2

Based on the post, https://stackoverflow.com/a/6807178/152852, the additional gem "net-ssh-telnet" provides the exact behavior that I am looking for.

require 'net/ssh'
require 'net/ssh/telnet'

session = Net::SSH.start(host, user, :password => password)
t = Net::SSH::Telnet.new("Session" => session, "Prompt" => prompt)

puts t.cmd 'configure'
puts t.cmd 'show | compare'
puts t.cmd 'exit'
puts t.cmd 'exit'
Community
  • 1
  • 1
uhleeka
  • 698
  • 8
  • 19
  • is there anyway to avoid specifying the "Prompt" ? If I do not correctly specify the "Prompt", my script will hang. – TX T Mar 20 '13 at 17:22
  • You don't have to specify the prompt it comes with a default, and yours might match the default. – Lifeweaver May 02 '13 at 15:09
  • thanks, the Prompt thingy and script hanging kind of puzzled me a lot for a while, until I had a look at your post. +1'd – Nikhil Mulley Jun 19 '14 at 07:27
1

I know this is a super old question but for reference I've found two ruby libraries that wrap telnet/ SSH sessions specifically for Cisco/ Juniper switch automation:

thom_nic
  • 7,809
  • 6
  • 42
  • 43