【问题标题】:Net::SSH with non unix/linux host?Net::SSH 与非 unix/linux 主机?
【发布时间】:2012-06-18 20:53:07
【问题描述】:

我正在尝试使用 Net::SSH 库来登录和管理支持 ssh 的主机。它是一种电信设备,TL1 也是如此。我似乎能够成功登录,但是当我尝试 ssh.exec 某些东西时,它中止说它无法执行命令。这是我的简单代码:

require 'net/ssh'

Net::SSH.start('10.204.121.192', 'password', :password => "password") do |ssh|
  ssh.exec("INH-MSG-ALL;")
end

如果我将相同的代码指向 Linux 服务器并提供诸如“ls -l /”之类的命令,它就可以正常工作。我想知道的是,我可以使用这个 ssh 库吗?我需要使用其他命令而不是 exec 吗?

这是错误输出:

/usr/local/rvm/gems/ruby-1.9.2-p290/gems/net-ssh-2.2.1/lib/net/ssh/connection/session.rb:322:in `block (2 levels) in exec': could not execute command: "INH-MSG-ALL;" (RuntimeError)
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/net-ssh-2.2.1/lib/net/ssh/connection/channel.rb:597:in `call'
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/net-ssh-2.2.1/lib/net/ssh/connection/channel.rb:597:in `do_failure'
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/net-ssh-2.2.1/lib/net/ssh/connection/session.rb:586:in `channel_failure'
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/net-ssh-2.2.1/lib/net/ssh/connection/session.rb:456:in `dispatch_incoming_packets'
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/net-ssh-2.2.1/lib/net/ssh/connection/session.rb:213:in `preprocess'
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/net-ssh-2.2.1/lib/net/ssh/connection/session.rb:197:in `process'
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/net-ssh-2.2.1/lib/net/ssh/connection/session.rb:161:in `block in loop'
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/net-ssh-2.2.1/lib/net/ssh/connection/session.rb:161:in `loop'
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/net-ssh-2.2.1/lib/net/ssh/connection/session.rb:161:in `loop'
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/net-ssh-2.2.1/lib/net/ssh/connection/session.rb:110:in `close'
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/net-ssh-2.2.1/lib/net/ssh.rb:194:in `start'
from ssh_test.rb:3:in `<main>'

【问题讨论】:

    标签: ruby


    【解决方案1】:

    我认为当您手动登录 shell 时它可以正常工作。

    要了解当您通过 net/ssh 连接时收集env 命令在两种情况下的输出并进行比较时有什么区别。

    你很可能会看到一个不同之处,它会引导你找到解决方案,或者至少会给你带来肮脏的把戏。

    更新。 (不工作)

    Net::SSH.start('10.204.121.192', 'password', :password => "password") do |ssh|
       ssh.open_channel do |channel|
            channel.on_data do |ch, data|
              puts "got data: #{data.inspect}"
            end
            channel.send_data("INH-MSG-ALL;\n")
       end
    end
    

    更新2。 (工作中)

    Net::SSH.start('10.204.121.192', 'password', :password => "password") do |ssh|
       ssh.open_channel do |channel|
            channel.send_channel_request "shell"
            channel.on_data do |ch, data|
              puts "got data: #{data.inspect}"
            end
            channel.send_data("INH-MSG-ALL;\n")
       end
    end
    

    【讨论】:

    • 好吧,我不太清楚你在 ruby​​ 中的“env”命令是什么意思?还是在我进入的主机上?如果是后者,我 ssh 进入的设备上没有 env 命令,因为它只说 TL1。我确实尝试使用 pp 打印出 ruby​​ 中的 ENV 以查看是否有变化,但它没有。
    • 还有另一种方法send_data。试试看。
    • 最后不要忘记\n,因为很可能它是在 ssh 连接上生成的一些二进制文件,因此您实际上不是在对 shell 而是对其他二进制输入/输出/错误。
    • 嗯,Net::SSH::Connection::Session 只有 send_message,我试过了。它没有中止,但似乎什么也没发生,我不知道如何读取输出,如果有的话。我确实看到了一个频道的 send_data,所以我也尝试了这个 channel = ssh.open_channel do |ch| ch.send_data("INH-MSG-ALL;\n") end 但这只是挂起
    • 嗯,这也挂了,这是我的代码:channel = ssh.open_channel do |ch| channel.on_data do |ch, data| puts "got data: #{data.inspect}" end channel.send_data("INH-MSG-ALL;\n") end
    【解决方案2】:

    感谢 forker 的更新),

    还有一件事,

    从你的代码中如何做到这一点

    puts "got data: #{data.inspect}"
    

    为每个发送到 shell 的命令输出数据?

    此代码是否等待每个命令完成?

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-02
      • 2010-12-26
      • 2022-01-25
      • 2015-07-09
      • 1970-01-01
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多