Using ruby gem net-ssh to configure a Juniper router

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 06:27:45

问题


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


回答1:


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'



回答2:


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:

  • expect4r (Cisco iOS/ Juniper JunOS)
  • ruby-cisco


来源:https://stackoverflow.com/questions/10327141/using-ruby-gem-net-ssh-to-configure-a-juniper-router

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!