ssh代理脚本在ruby中运行但不在jruby

时间:2016-04-04 12:00:25

标签: ruby jruby net-ssh

我有一个使用net :: ssh gem的ruby脚本。

它是一个简单的脚本,用于将跳转服务器隧道传送到目标框。该框仅对跳出服务器可见。这是脚本:

require 'net/ssh/proxy/command'
require "net/ssh"

class TestTunnel
  def initialize
    proxy = Net::SSH::Proxy::Command.new('ssh admin@10.10.10.10 nc %h %p 2>/dev/null')
    p "proxy gotten"
    Net::SSH.start("host1", "admin", :password => "admin", :proxy => proxy) do |ssh|
      hostname = ssh.exec!("hostname")
      p "In #{hostname}"
    end
  end
end
b=TestTunnel.new

问题是脚本在ruby中运行但不在jruby中运行。我的框架运行jruby所以我需要使用jruby。

我的解决方案是从我的jruby脚本调用bash脚本来设置隧道并在那里运行命令,但我宁愿在jruby中完成所有操作。

以下是我得到的错误:

~/src/main/ruby$ jruby --1.9 scripts/test_tunnel.rb 
"proxy gotten"
IO::EAGAINWaitReadable: Resource temporarily unavailable - errno backtraces disabled; run with -Xerrno.backtrace=true to enable

当我在ruby中跑步时:

~/src/main/ruby$ ruby scripts/test_tunnel.rb 
"proxy gotten"
"In vz-int-api02\n"
~/src/main/ruby$ 

当我使用-Xerrno.backtrace = true运行时,我可以看到jruby是1.7.9,我可以看到它使用ruby 1.9:

~/src/main/ruby$ jruby -Xerrno.backtrace=true --1.9 scripts/test_tunnel.rb
"proxy gotten"
IO::EAGAINWaitReadable: Resource temporarily unavailable - 
  read_nonblock at org/jruby/RubyIO.java:2856
           recv at /usr/local/jruby-1.7.9/lib/ruby/gems/shared/gems/net-ssh-2.9.2/lib/net/ssh/proxy/command.rb:75
           fill at /usr/local/jruby-1.7.9/lib/ruby/gems/shared/gems/net-ssh-2.9.2/lib/net/ssh/buffered_io.rb:65
    next_packet at /usr/local/jruby-1.7.9/lib/ruby/gems/shared/gems/net-ssh-2.9.2/lib/net/ssh/transport/packet_stream.rb:88
   poll_message at /usr/local/jruby-1.7.9/lib/ruby/gems/shared/gems/net-ssh-2.9.2/lib/net/ssh/transport/session.rb:183
           loop at org/jruby/RubyKernel.java:1519
   poll_message at /usr/local/jruby-1.7.9/lib/ruby/gems/shared/gems/net-ssh-2.9.2/lib/net/ssh/transport/session.rb:178
           wait at /usr/local/jruby-1.7.9/lib/ruby/gems/shared/gems/net-ssh-2.9.2/lib/net/ssh/transport/session.rb:215
           loop at org/jruby/RubyKernel.java:1519
           wait at /usr/local/jruby-1.7.9/lib/ruby/gems/shared/gems/net-ssh-2.9.2/lib/net/ssh/transport/session.rb:213
     initialize at /usr/local/jruby-1.7.9/lib/ruby/gems/shared/gems/net-ssh-2.9.2/lib/net/ssh/transport/session.rb:87
          start at /usr/local/jruby-1.7.9/lib/ruby/gems/shared/gems/net-ssh-2.9.2/lib/net/ssh.rb:207
     initialize at scripts/test_tunnel.rb:8
     (root) at scripts/test_tunnel.rb:15
~/src/main/ruby$ jruby -Xerrno.backtrace=true --1.9 --version
jruby 1.7.9 (1.9.3p392) 2013-12-06 87b108a on Java HotSpot(TM) 64-Bit Server VM 1.8.0_72-b15 +indy [linux-amd64]
~/src/main/ruby$

有没有办法让它像jruby一样在jruby中运行,或者我是从jruby中调用bash脚本的唯一方法吗?

A

1 个答案:

答案 0 :(得分:2)

这听起来很像net-ssh中的this bug,它在net-ssh 3.0.2中得到修复。

关闭该问题的提交消息是“错误修正:代理命令正在使用非阻塞io api错误地导致罕见的IO :: EAGAIN错误”,这听起来就像你得到的那样。