2010-06-02 79 views
3

我正在使用本地到远程端口转发从我的Windows框中访问远程数据库。它像使用putty进行端口转发的魅力一样工作,但当我尝试使用Ruby/Net :: SSH转发时,它失败。这里是我的代码片段:使用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的端口在使用浏览器进行测试时工作正常。 端口转发到我的Linux服务器,我的数据库服务器正在侦听端口1234,无法正常工作。当我尝试连接到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 

然后没事!

我使用的净SSH 2.0.22 /红宝石1.8.7

+0

尝试评论谷歌前进,只是做一个转发。这有帮助吗? – barrycarter 2010-06-02 17:10:57

+0

不幸的是它不起作用。 – nakhli 2010-06-03 08:29:45

回答

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),我可以连接。

尝试这种情况:

​​

注意,“本地主机”在这种情况下被解释为相对于所述主机到你通过ssh连接,即远程主机。

+0

我会试试,谢谢。 – nakhli 2012-04-05 16:21:03

相关问题