使用ruby gem net-ssh配置Juniper路由器 [英] Using ruby gem net-ssh to configure a Juniper router

查看:217
本文介绍了使用ruby gem net-ssh配置Juniper路由器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想能够使用ruby net-ssh gem来更改Juniper M10i路由器。但是,发送configure后,我无法发送任何配置命令。



例如,在通过ssh登录路由器后,以下三个命令:

 配置
显示系统
退出

使用net-ssh库,我试过下面的没有成功:

 #net-ssh(2.3.0)
需要'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 |
put频道已打开。

ch.on_close do | ch |
putsChannel is closing!
end

ch.on_data do | ch,data |
putsData#{data} !!!!!!!!!!
end

ch.execconfiguredo | ch,success |
将FAIL:configure除非成功
end

ch.execshow systemdo | ch,success |
输入FAIL:show system,除非成功
end

ch.execexitdo | ch,success |
将FAIL:exit除非成功
end
end
session.loop

$ b b

执行后,我得到以下输出:

  
FAIL:show system
FAIL:exit
数据输入配置模式
!!!!!!!!!!
频道正在关闭!那么如何在configure之后正确地传递show system命令呢?



已解决:



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

解决方案

根据帖子, http://stackoverflow.com/a/6807178/152852 ,额外的宝石 net-ssh-telnet 提供了我正在寻找的确切行为。

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

session = Net :: SSH.start(host,user,:password => password)
t = :: SSH :: Telnet.new(Session=> session,Prompt=>提示符)

puts t.cmd'configure'
puts t.cmd'show |比较'
puts t.cmd'exit'
puts t.cmd'exit'


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: http://stackoverflow.com/a/6807178/152852

解决方案

Based on the post, http://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'

这篇关于使用ruby gem net-ssh配置Juniper路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆