Remote port forwarding using Ruby / Net :: SSH for remote db connection

I am accessing a remote database using local and remote port forwarding from a window window. It works like a charm, using putty for port forwarding, but it doesn't work when I try to redirect using 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 sent to google.com works great when testing using a browser. The port redirected to my Linux server where my db server is listening on port 1234 does not work. When I try to connect to localhasot: 4444 the connection freezes. Magazines:

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 am using net-ssh 2.0.22 / ruby ​​1.8.7

+3
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), .

:

Net::SSH.start(remote_host, user_name, :password => password) do |ssh|
  ssh.forward.local(4444, 'localhost', 1234)
  ssh.loop { true }
end

, "localhost" , ssh, .. .

+1

Source: https://habr.com/ru/post/1748198/


All Articles