我正在使用Windows框转发局部到示例端口访问远程数据库。它的工作就像是使用腻子进行端口转发的魅力,但是当我尝试使用ruby/net :: ssh转发时,它会失败。这是我的代码段:

require 'rubygems'
require 'net/ssh'

Net::SSH.start(remote_host, user_name, :password => password) do |ssh|
  ssh.logger.sev_threshold=Logger::Severity::DEBUG
  ssh.forward.local(5555, 'www.google.com', 80) # works perfectly
  ssh.forward.local(4444, remote_host, 1234)    # db connection hangs up
  ssh.loop { true }
end

使用浏览器测试时,端口向Google.com转发正常工作。我的DB服务器在端口1234上侦听的Linux Server的端口转发到我的Linux Server不起作用。当我尝试连接到localhasot时:4444连接挂起。日志:

DEBUG -- net.ssh.service.forward[24be0d4]: received connection on 127.0.0.1:4444
DEBUG -- tcpsocket[253ba08]: queueing packet nr 6 type 90 len 76
DEBUG -- tcpsocket[24bde64]: read 8 bytes
DEBUG -- tcpsocket[253ba08]: sent 100 bytes
DEBUG -- net.ssh.connection.channel[24bdcc0]: read 8 bytes from client, sending over local forwarded connection

然后什么都没有!

我正在使用Net-SSH 2.0.22 / Ruby 1.8.7

有帮助吗?

解决方案

更改第二个 remote_host'localhost'. 。您的数据库服务器可能仅绑定到127.0.0.1(localhost),但是您的SSH向前将数据包发送到您的外部IP地址(通过解决方案获得 remote_host).

SSH到Linux框并运行 sudo netstat -n --tcp --listen -p. 。例如,我看到了:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
...
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      1552/postgres

这意味着Postgres仅在127.0.0.1上听(值 Local Address)。如果我运行命令 psql -h 127.0.0.1, ,我可以连接到我的数据库。但是如果我运行命令 psql -h <hostname> 或者 psql -h <my external IP>, ,连接被拒绝。

此外,如果我有 ssh.forward.local(4444, remote_host, 5432), ,我无法通过端口向前连接到我的数据库。但是如果我有 ssh.forward.local(4444, 'localhost', 5432), ,我能够连接。

尝试这个:

Net::SSH.start(remote_host, user_name, :password => password) do |ssh|
  ssh.forward.local(4444, 'localhost', 1234)
  ssh.loop { true }
end

请注意,在这种情况下,“ Localhost”是根据您通过SSH(即远程主机)连接到的主机来解释的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top