【问题标题】:Using ruby gem net-ssh to configure a Juniper router使用 ruby​​ gem net-ssh 配置 Juniper 路由器
【发布时间】:2016-08-12 04:23:56
【问题描述】:

我希望能够使用 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!

那么如何在“configure”之后正确传递“show system”命令呢?

已解决:

我偶然发现了以下帖子:https://stackoverflow.com/a/6807178/152852

【问题讨论】:

    标签: ruby-on-rails-3 configuration router net-ssh


    【解决方案1】:

    根据帖子 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'
    

    【讨论】:

    • 是否有避免指定“提示”?如果我没有正确指定“提示”,我的脚本将会挂起。
    • 您不必指定它附带的默认提示,您的提示可能与默认匹配。
    • 谢谢,Prompt thingy 和 script hang 让我很困惑一段时间,直到我看了你的帖子。 +1 了
    【解决方案2】:

    我知道这是一个非常古老的问题,但作为参考,我发现了两个专门为 Cisco/Juniper 交换机自动化包装 telnet/SSH 会话的 ruby​​ 库:

    【讨论】:

      猜你喜欢
      • 2014-03-08
      • 1970-01-01
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      • 2022-08-12
      • 1970-01-01
      • 1970-01-01
      • 2017-03-01
      相关资源
      最近更新 更多