3

我正在使用 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 服务器的端口不起作用。当我尝试连接到 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

4

1 回答 1

1

将第二个更改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 连接的主机(即远程主机)进行解释的。

于 2012-04-05T13:09:47.133 回答