[英]Local-to-remote port forwarding using Ruby/Net::SSH for remote db connection
I'm accessing a remote database using a local-to-remote port forwarding from my windows box. 我正在使用从我的Windows框转发的本地到远程端口访问远程数据库。 It works like a charm using putty for port forwarding but it fails when I try to forward using Ruby/Net::SSH.
它就像一个使用putty进行端口转发的魅力,但是当我尝试使用Ruby / Net :: SSH转发时它失败了。 Here is my code snippet:
这是我的代码片段:
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
The port forward to google.com works fine when tested with a browser. 使用浏览器进行测试后,google.com的前端工作正常。 The port forward to my linux server, where my db server is listening on port 1234, isn't working.
转发到我的linux服务器的端口,我的数据库服务器正在端口1234上侦听,但是无效。 When I try to connect to localhasot:4444 the connection hangs up.
当我尝试连接到localhasot时:4444连接挂起。 Logs:
日志:
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
And then nothing! 然后什么都没有!
I'm using net-ssh 2.0.22 / ruby 1.8.7 我正在使用net-ssh 2.0.22 / ruby 1.8.7
Change the second remote_host
to 'localhost'
. 将第二个
remote_host
更改为'localhost'
。 Your database server is probably only bound to 127.0.0.1 (localhost), but your SSH forward is sending packets to your external IP address (obtained by resolving remote_host
). 您的数据库服务器可能只绑定到127.0.0.1(localhost),但您的SSH转发是将数据包发送到您的外部IP地址(通过解析
remote_host
获得)。
Ssh to the linux box and run sudo netstat -n --tcp --listen -p
. ssh到linux框并运行
sudo netstat -n --tcp --listen -p
。 For example, I see this: 例如,我看到了这个:
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
which implies postgres is only listening on 127.0.0.1 (value of Local Address
). 这意味着postgres只收听127.0.0.1(
Local Address
值)。 If I run the command psql -h 127.0.0.1
, I can connect to my database. 如果我运行命令
psql -h 127.0.0.1
,我可以连接到我的数据库。 But if I run the command psql -h <hostname>
or psql -h <my external IP>
, the connection is refused. 但是,如果我运行命令
psql -h <hostname>
或psql -h <my external IP>
,则拒绝连接。
Furthermore, if I have ssh.forward.local(4444, remote_host, 5432)
, I am unable to connect to my database via the port forward. 此外,如果我有
ssh.forward.local(4444, remote_host, 5432)
,我无法通过端口转发连接到我的数据库。 But If I have ssh.forward.local(4444, 'localhost', 5432)
, I am able to connect. 但如果我有
ssh.forward.local(4444, 'localhost', 5432)
,我能够连接。
Try this: 尝试这个:
Net::SSH.start(remote_host, user_name, :password => password) do |ssh|
ssh.forward.local(4444, 'localhost', 1234)
ssh.loop { true }
end
Note that 'localhost' in this case is interpreted relative to the host to which you connected via ssh, ie the remote host. 请注意,在这种情况下,“localhost”是相对于您通过ssh连接的主机(即远程主机)进行解释的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.