问题
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