简体   繁体   English

带有Net :: SSH的多个SSH跃点(Ruby)

[英]Multiple SSH hops with Net::SSH (Ruby)

So here's my setup: 所以这是我的设置:

Laptop -> Host 1 -> Host 2 -> Host 3 笔记本电脑->主机1->主机2->主机3

Laptop can reach Host 1, but not Host 2 or Host 3 便携式计算机可以到达主机1,但不能到达主机2或主机3
Host 1 can reach Host 2, but not Host 3 主机1可以到达主机2,但不能到达主机3
Host 3 can reach Host 2, but not Host 1 主机3可以到达主机2,但不能到达主机1

What I'm trying to do is set up remote forwards so that a process running on Host 3 will be routed to running service on Laptop. 我想做的是设置远程转发,以便在主机3上运行的进程将被路由到笔记本电脑上正在运行的服务。 I've successfully done this using the following code: 我已经使用以下代码成功完成了此操作:

Run from Laptop: 从笔记本电脑运行:

require 'rubygems'
require 'net/ssh'

threads = []
config = {:user => "user", :remote_port => 3333, :service_port => 2222}

threads << Thread.new{
Net::SSH.start("host1", config[:user]) do |ssh|
  puts "Forwarding port #{config[:remote_port]} on host1 to #{config[:service_port]} on localhost"
  ssh.forward.remote(config[:service_port], "localhost", config[:remote_port], "127.0.0.1")
  ssh.exec! "ssh #{config[:user]}@host2 -R #{config[:remote_port]}:localhost:#{config[:remote_port]}"
  ssh.loop {true}
end
}

threads << Thread.new{
Net::SSH.start("host3", config[:user]) do |ssh|
  puts "Creating local forward for port #{config[:service_port]} on host3 to port #{config[:remote_port]} on host2"
  ssh.exec! "ssh #{config[:user]}@host2 -L #{config[:service_port]}:localhost:#{config[:remote_port]}"
  ssh.loop {true}
end
}

threads.each {|t| t.join}

In one thread, I'm setting up a remote forward from Laptop to Host 1 and then another remote forward from Host 1 to Host 2. In a separate thread, I'm starting another connection from Laptop to Host 3, then running a local forward from Host 3 to Host 2. 在一个线程中,我要设置一个从笔记本电脑到主机1的远程转发,然后在另一个远程主机中,将它从主机1转发到主机2。在另一个线程中,我要启动从笔记本电脑到主机3的另一个连接,然后运行本地从主机3转发到主机2。

The only way I can connect from Laptop to Host 3 is because of my .ssh/config file, which automatically routes me through Host 1 and Host 2 when I try to connect to Host 3 from Laptop. 我可以从笔记本电脑连接到主机3的唯一方法是因为我的.ssh / config文件,当我尝试从笔记本电脑连接到主机3时,该文件会自动将我路由到主机1和主机2。

What I want to do is cut out the second thread where I'm connecting from Laptop to Host 3 so that I can remove the dependency on the .ssh/config file. 我想做的是切出我要从笔记本电脑连接到主机3的第二个线程,以便可以删除对.ssh / config文件的依赖性。 I want to do all of my connections from within the first thread. 我想在第一个线程中进行所有连接。

So basically I need to do multiple hops that originate from Laptop. 因此,基本上,我需要进行多个源自笔记本电脑的跃点。 I can initiate the first connection from Laptop to Host 1 and then execute a command on Host 1, but after that I can't get any further. 我可以启动从笔记本电脑到主机1的第一个连接,然后在主机1上执行命令,但是此后我无法再进行任何操作了。 What I need to do is initiate the connection from Laptop to Host 1, set up the forward on Host 1, connect to Host 2 from Host 1 and then set up the second forward on Host 2. 我需要做的是启动从笔记本电脑到主机1的连接,在主机1上设置转发,从主机1连接到主机2,然后在主机2上设置第二个转发。

Is this possible to do with net/ssh? 这可能与net / ssh有关吗?

Thanks for your help! 谢谢你的帮助!

I fixed this by writing out an SSH config file, then specifying the config file when initiating the connection. 我通过写出SSH配置文件,然后在启动连接时指定配置文件来解决此问题。 The config file contains proxy commands that will automatically route through the necessary hosts to reach my destination. 配置文件包含代理命令,这些命令将自动路由必要的主机到达我的目的地。

Example: 例:

def write_config_file

    File.open('confi_file', 'w') { |f|
      f << "host host1\n"
      f << "HostName host1.something.net\n"
      f << "user user_name\n"
      f << "\n"
      f << "host host2\n"
      f << "HostName host2.something.net\n"
      f << "ProxyCommand ssh host1 nc %h %p 2> /dev/null\n"
      f << "user user_name\n"
      f << "\n"
      f << "host host3\n"
      f << "HostName host3.something.net\n"
      f << "ProxyCommand ssh host2 nc %h %p 2> /dev/null\n"
      f << "user user_name\n"
    } 

end

write_config_file

Net::SSH.start("host3", "user_name", :config => './config_file') do |ssh|
  #whatever you need to do...
end

I wrapped the connection in a begin/rescue blocked and trapped ctrl+c input, and in the trap block I delete the config file and shut down the connection. 我将连接包装在一个开始/救援阻止和捕获的ctrl + c输入中,在陷阱块中,我删除了配置文件并关闭了连接。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM