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) に送信しています。

Linux ボックスに SSH で接続して実行します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 に答える