我希望能够使用 Ruby net-ssh gem 对瞻博网络 M10i 路由器进行更改。 但是,发送"配置"后,我无法发送任何配置命令。
例如,通过ssh登录路由器后,我想发出以下三个命令:
configure
show system
exit
使用 net-ssh 库,我尝试了以下方法但没有成功:
# 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
执行时,我得到以下输出:
Channel open.
FAIL: show system
FAIL: exit
Data Entering configuration mode
!!!!!!!!!!
Channel is closing!
那么如何在"配置"之后正确传递"显示系统"命令呢?
解决:
我偶然发现了以下帖子:https://stackoverflow.com/a/6807178/152852
https://stackoverflow.com/a/6807178/152852,根据帖子,额外的 gem "net-ssh-telnet" 提供了我正在寻找的确切行为。
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'
这是一个超级老的问题,但作为参考,我找到了两个专门用于思科/瞻博网络交换机自动化的 telnet/SSH 会话的 ruby 库:
- expect4r (Cisco iOS/Juniper JunOS)
- 红宝石-思科