使用Net :: SSH :: Gateway设置隧道(挂在.new上) [英] using Net::SSH::Gateway to setup a tunnel (hung at .new)

查看:118
本文介绍了使用Net :: SSH :: Gateway设置隧道(挂在.new上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像使用net/ssh/gateway一样建立到mysql的ssh隧道,我试图使用Net :: SSH :: Gateway设置到远程服务器的隧道以进行端口转发.但是我会在此过程的早期陷入困境! :)

Just as in Using net/ssh/gateway to establish ssh tunnel to mysql, I'm trying to use Net::SSH::Gateway set up a tunnel to a remote server for port forwarding. But I'm getting stuck earlier in the process! :)

我该如何设置OpenSSL和Net :: SSH :: Gateway.new()以隧道传输到远程服务器?

How do I condition OpenSSL and Net::SSH::Gateway.new() for tunneling to a remote server?

可以通过在后台启动ssh进程来建立隧道,如下所示:

I can set up a tunnel by starting an ssh process in the background as follows:

remote_addr = <some_remote_server.com>
remote_user = <some_remote_server@somehost.com>
ssh -f -N -L 3307:#{remote_addr}:3306 #{remote_user}

以及在我的rails代码中:

and in my rails code:

# config/database.yml
...
remote_db:
    adapter: mysql2
    database: <remote_db_name>
    username: <remote_db_user>
    password: <remote_db_pass>
    host: 127.0.0.1
    port: 3307

这一切正常.

但是,当我尝试以下操作时(当然,没有在后台启动ssh),它会失败并显示拒绝连接":

But when I try the following (without launching ssh in the background, of course), it fails with 'connection refused':

>> require 'net/ssh/gateway'
>> remote_addr = <some_remote_server.com>
>> remote_user = <some_remote_server@somehost.com>
>> gateway = Net::SSH::Gateway.new(remote_addr, remote_user, :port => 3306, :verbose => :debug)
D, [2014-12-04T07:56:50.720603 #32532] DEBUG -- net.ssh.transport.session[3fe0d1587180]: establishing connection to some_remote_server.com:3306
... then after a delay ...
Errno::ECONNREFUSED: Connection refused - connect(2) for "some_remote_server.com" port 3306

我也尝试了不使用端口规格(因此它使用端口22),导致超时:

I also tried it without the port spec (so it used port 22), which results in a timeout instead:

>> gateway = Net::SSH::Gateway.new(remote_addr, remote_user, :verbose => :debug)
D, [2014-12-04T07:59:17.722649 #32532] DEBUG -- net.ssh.transport.session[3fe0d156efb8]: establishing connection to some_remote_server.com:22
Errno::ETIMEDOUT: Operation timed out - connect(2) for "some_remote_server.com" port 22

键的位置

我怀疑OpenSSL找不到我的证书.当我在详细模式下运行ssh时,可以在〜/.ssh/id_rsa中看到它提供并接受我的公钥.但是,即使我明确地将密钥文件作为参数传递:

location of the keys

I have a suspicion that perhaps OpenSSL isn't finding my certificates. When I run ssh in verbose mode, I can see it offering and accepting my public key in ~/.ssh/id_rsa. But even when I explicitly passed my key file as an argument:

gateway = Net::SSH::Gateway.new(remote_addr, 
                                remote_user,
                                :keys => ['/Users/home/.ssh/id_rsa'], 
                                :port => 3306, 
                                :verbose => :debug)

...似乎没有什么改变.

... it didn't seem to make a difference.

  • 我应将哪些参数传递给Net::SSH:Gateway.new(),以便它模拟对ssh -N -L ...的调用?
  • 如果由于OpenSSL找不到我的证书而收到ECONNREFUSED或ETIMEDOUT响应,如何告诉它在哪里查看?
  • What arguments do I pass to Net::SSH:Gateway.new() so that it emulates the call to ssh -N -L ...?
  • If I'm getting the ECONNREFUSED or ETIMEDOUT responses because OpenSSL can't find my certs, how do I tell it where to look?

推荐答案

好的,这是一个简单的修复问题.尽管我很尴尬,但我还是发布了解决方案,以防其他人觉得它有用.

Okay, it was a boneheaded problem with a simple fix. Despite my embarrassment, I'm posting the solution in case someone else finds it useful.

简而言之,如果您一直在像这样运行ssh:

In short, if you've been running ssh like this:

remote_addr = <some_remote_server.com>
remote_user = <some_remote_user@somehost.com>
ssh -f -N -L 3307:#{remote_addr}:3306 #{remote_user}

您可能想要:

 require 'net/ssh/gateway'
 gateway = Net::SSH::Gateway.new("somehost.com", "some_remote_user")
 port = gateway.open(remote_addr, 3306, 3307)

我在O.P.中得到ECONNREFUSED和ETIMEDOUT的原因仅仅是因为我试图连接到错误的服务器.

The reason I was getting ECONNREFUSED and ETIMEDOUT in the O.P. was simply because I was trying to connect to the wrong server.

这篇关于使用Net :: SSH :: Gateway设置隧道(挂在.new上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆