简体   繁体   English

如何使用 Ruby 进行动态端口转发

[英]How to do dynamic port forwarding using Ruby

How do I do SSH dynamic port forwarding on Ruby?如何在Ruby上做SSH动态端口转发?

I tried to use gems such as "net/ssh/socks" and "net/ssh/gateway", but it looks like they are already outdated because I can't even require them.我尝试使用诸如“net/ssh/socks”和“net/ssh/gateway”之类的 gem,但看起来它们已经过时了,因为我什至不需要它们。

All I need to do is run this shell command我需要做的就是运行这个 shell 命令

ssh -D 5555 user@host -f -N

and receive the PID of this process.并接收该进程的PID。

I also tried to use Kernel#system and Kernel#spawn .我还尝试使用Kernel#systemKernel#spawn

For example:例如:

pid = spawn("ssh -D 5555 user@host -f -N")
Process.wait pid

This works fine for me, but the PID is always returned 3 units less than the real PID of the process that started.这对我来说很好,但是返回的 PID 总是比启动的进程的实际 PID 少 3 个单位。 For example, the returned PID is 1555 and the real PID is 1558 .例如,返回的 PID 为1555 ,真实的 PID 为1558

Another question: why is the difference in the PID always 3 and can there be another difference?另一个问题:为什么 PID 的差异总是 3,还有其他差异吗? Can I use pid + 3 reliably?我可以可靠地使用pid + 3吗?

Using the SSH binary使用 SSH 二进制文件

You are running SSH with -f (fork).您正在使用-f (fork)运行 SSH。 In Ruby, Kernel.spawn will automatically fork the process into the background, so you do not need to use -f .在 Ruby 中, Kernel.spawn会自动将进程分叉到后台,所以你不需要使用-f If you use -f you will create a double fork situation, which is the reason for the PID anomaly.如果使用-f会造成双叉的情况,这就是 PID 异常的原因。

This should be enough to make it work:这应该足以使其工作:

pid = spawn("ssh -N -D 5555 user@host")

Using Net::SSH with Net::SSH::Socks使用 Net::SSH 和 Net::SSH::Socks

If you want to use net-ssh-socks you will have to clone the repository manually, since the Rubygems version has not been updated for the latest Net::SSH.如果要使用net-ssh-socks ,则必须手动克隆存储库,因为 Rubygems 版本尚未更新为最新的 Net::SSH。

git clone https://github.com/cristianbica/net-ssh-socks

Then your Ruby code would look like this, assuming you cloned into the same folder that your script is running in:然后你的 Ruby 代码看起来像这样,假设你克隆到你的脚本正在运行的同一个文件夹中:

$:.push('net-ssh-socks/lib')  # Add the cloned repo to load path
require 'net/ssh/socks'

ssh = Net::SSH.start('host', 'user')
ssh.forward.socks(5555)
ssh.loop { true }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用从 ruby 开始的 SSM 端口转发 session 的结果? - How do I use the results of an SSM port forwarding session started with ruby? 你如何使用Ruby找到一个免费的TCP服务器端口? - How do you find a free TCP server port using Ruby? 使用Ruby / Net :: SSH进行远程数据库连接的本地到远程端口转发 - Local-to-remote port forwarding using Ruby/Net::SSH for remote db connection 使用 Ruby Sinatra:4567 / Shotgun:9292 的 Virtual Box 端口转发问题 - Virtual Box port forwarding misery with Ruby Sinatra:4567 / Shotgun:9292 如何在Windows中使用Ruby打开端口? - How to open a port in Windows using Ruby? 你如何在Ruby中找到一个随机的开放端口? - How do you find a random open port in Ruby? 如何使用动态语言工具包(DLTK)将Ruby文件添加到Eclipse 3.6中的现有项目中? - How do you add a ruby file to an existing project in Eclipse 3.6 using the Dynamic Languages Toolkit (DLTK)? 如何在轨道上使用 ruby 声明动态数组 - how to declare a dynamic array using ruby on rails 如何查找在ruby中使用TcpServer.new(0)启动的TcpServer的端口 - How to find the port of a TcpServer started using TcpServer.new(0) in ruby vagrantfile中的条件端口转发 - Conditional port forwarding in vagrantfile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM