繁体   English   中英

使用Ruby / Net :: SSH进行远程数据库连接的本地到远程端口转发

[英]Local-to-remote port forwarding using Ruby/Net::SSH for remote db connection

我正在使用从我的Windows框转发的本地到远程端口访问远程数据库。 它就像一个使用putty进行端口转发的魅力,但是当我尝试使用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

然后什么都没有!

我正在使用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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM