2010-06-02 31 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

相關問題