ruby - 如何正确实现 Net::SSH 端口转发

标签 ruby port forward portforwarding net-ssh

我一直在尝试让端口转发与 Net::SSH 一起正常工作。据我了解,如果我希望能够从同一个 Ruby 程序中使用它,我需要 fork 出 Net::SSH session ,以便事件处理循环实际上可以处理通过连接发送的数据包。但是,这会导致您在以下内容中看到的丑陋之处:

#!/usr/bin/env ruby -w
require 'net/ssh'
require 'httparty'
require 'socket'
include Process

log = Logger.new(STDOUT)
log.level = Logger::DEBUG

local_port = 2006
child_socket, parent_socket = Socket.pair(:UNIX, :DGRAM, 0)
maxlen = 1000
hostname = "www.example.com"

pid = fork do
  parent_socket.close
  Net::SSH.start("hostname", "username") do |session|
    session.logger = log
    session.logger.sev_threshold=Logger::Severity::DEBUG
    session.forward.local(local_port, hostname, 80)
    child_socket.send("ready", 0)
    pidi = fork do
      msg = child_socket.recv(maxlen)
      puts "Message from parent was: #{msg}"
      exit
    end
    session.loop do
      status = waitpid(pidi, Process::WNOHANG)
      puts "Status: #{status.inspect}"
      status.nil?
    end
  end
end

child_socket.close

puts "Message from child: #{parent_socket.recv(maxlen)}"
resp = HTTParty.post("http://localhost:#{local_port}/", :headers => { "Host" => hostname } )
# the write cannot be the last statement, otherwise the child pid could end up
# not receiving it
parent_socket.write("done")
puts resp.inspect

谁能告诉我一个更优雅/更好的工作解决方案?

最佳答案

我花了很多时间试图弄清楚如何正确地实现端口转发,然后我从 net/ssh/gateway 库中获得灵感。我需要一个强大的解决方案,可以在各种可能的连接错误后运行。这是我现在正在使用的,希望对您有所帮助:

require 'net/ssh'

ssh_options = ['host', 'login', :password => 'password']
tunnel_port = 2222
begin
  run_tunnel_thread = true
  tunnel_mutex = Mutex.new
  ssh = Net::SSH.start *ssh_options
  tunnel_thread = Thread.new do
    begin
      while run_tunnel_thread do
        tunnel_mutex.synchronize { ssh.process 0.01 }
        Thread.pass
      end
    rescue => exc
      puts "tunnel thread error: #{exc.message}"
    end
  end
  tunnel_mutex.synchronize do
    ssh.forward.local tunnel_port, 'tunnel_host', 22
  end

  begin
    ssh_tunnel = Net::SSH.start 'localhost', 'tunnel_login', :password => 'tunnel_password', :port => tunnel_port
    puts ssh_tunnel.exec! 'date'
  rescue => exc
    puts "tunnel connection error: #{exc.message}"
  ensure
    ssh_tunnel.close if ssh_tunnel
  end

  tunnel_mutex.synchronize do
    ssh.forward.cancel_local tunnel_port
  end
rescue => exc
  puts "tunnel error: #{exc.message}"
ensure
  run_tunnel_thread = false
  tunnel_thread.join if tunnel_thread
  ssh.close if ssh
end

关于ruby - 如何正确实现 Net::SSH 端口转发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13998267/

相关文章:

ruby-on-rails - Rails local_assign 与局部变量

ssl - 如何通过端口 8443 上的 https 运行 tomcat7 网络应用程序?

android - 在 Mips 架构上运行 Android

java - 将 X 小时添加到日期和时间

jsf - 如何在操作方法中将请求转发到非 JSF 页面?

java - servlet 响应何时提交或刷新?

ruby-on-rails - rails geokit - 获取旅行/通勤时间

ruby-on-rails - 如何从 ruby​​ 父类(super class)中的方法访问子类中的当前 __FILE__

ruby-on-rails - 如何从图像中读取条形码

docker - 将 LAN IP 地址分配给与主机 IP 地址不同的 Docker 容器