【问题标题】:How to connect SSH.NET in MongoDB C#如何在 MongoDB C# 中连接 SSH.NET
【发布时间】:2019-06-12 11:11:17
【问题描述】:

我正在使用这个 SSH.NET 客户端 https://github.com/sshnet/SSH.NET/ 和 MongoDB C# 最新客户端。根据https://jira.mongodb.org/browse/CSHARP-1142,我正在尝试将 SSHStream 注入 MongoDB 客户端设置。我的 SSH 连接成功。但我无法通过 SSH 建立与 MongoDB 的连接。

   static void Main(string[] args)
    {
        MongoClientSettings settings = new MongoClientSettings
        {
            ClusterConfigurator = cb =>
            {
                cb.RegisterStreamFactory(CustomStreamFac);
            }   
        };
        MongoClient mongoClient = new MongoClient(settings);
        IMongoDatabase db = mongoClient.GetDatabase("testdb");
        var collections = db.ListCollections();
    }

    public static IStreamFactory CustomStreamFac(IStreamFactory istreamfac)
    {

        Stream stream = istreamfac.CreateStream();
        return istreamfac;
    }

public static class Extension
{
    public static Stream CreateStream(this IStreamFactory isf)
    {
        ConnectionInfo conn = new ConnectionInfo(hostname, port, username, new PrivateKeyAuthenticationMethod(username, new PrivateKeyFile(keyfile, password)));
        SshClient cli = new SshClient(conn);
        cli.Connect();
        ShellStream shellStream = null;
        shellStream = cli.CreateShellStream("Command", 0, 0, 0, 0, 1024);            
        return shellStream;
    }
}

感谢任何帮助。

【问题讨论】:

  • 1) 您可以使用与您的 C# 代码在同一台机器上运行的任何 SSH 客户端连接到 hostname:port 吗? 2) 你有任何理由相信CreateShellStream 应该能够与MongoDB 合作吗?我在 Jira 链接中没有找到任何证据。
  • @MartinPrikryl 是的,我可以使用host:port 连接到 SSH 服务器。不,他们没有提到任何关于CreateShellStream 的事情。我不确定它是否会处理,但我只看到此选项将 sshclient 转换为流以传递给IStreamFactory
  • 所以首先创建一个简单的控制台应用程序,它只通过 ssh 连接到 hostname:port。如果未连接,请向我们展示您的 SSH 客户端的日志文件以进行比较。 -- 您的直接问题与 MongoDB 无关。 -- 此外,您没有确认您在与 C# 代码相同的机器上运行 SSH 客户端。 - 你如何运行你的 C# 顺便说一句?
  • @MartinPrikryl 我没有 SSH 连接问题。我的 SSH 托管在 Amazon EC2 中,cli.Connect(); 创建了一个成功的连接。您指的 SSH 客户端是什么?我的 C# 控制台(SSH.NET)是我使用的客户端。为了确认 SSH 通信,我在 C# 代码所在的同一台机器上使用 RoboMongo 作为额外的 SSH 客户端并建立了通​​信。
  • 那么你从哪里得到“连接被拒绝异常”?不在 SSH 代码中?你在 MongoDB 代码中得到它吗?

标签: c# mongodb ssh ssh.net


【解决方案1】:

您可以使用以下代码启动 SSH 客户端。

ConnectionInfo conn = new ConnectionInfo(SSHServerHost, SSHServerPort,SSHServerUserName, new PrivateKeyAuthenticationMethod(SSHServerUserName, new PrivateKeyFile(PrivateKeyPath,SSHServerPassword)));
SshClient = new SshClient(conn);
SshClient.Connect();
ForwardedPortLocal forwardedPortLocal = new ForwardedPortLocal("127.0.0.1",5477,MongoDBHost, MongoDBPort);
SshClient.AddForwardedPort(forwardedPortLocal);
forwardedPortLocal.Start();

现在 SSH 客户端会将 localhost 端口 5477 转发到远程 MongoDB 数据库服务器。

MongoClient 可用于与 MongoDB Server 通信,主机为 localhost,端口为 5477。

参考以下代码 sn-p 连接 MongoDB Server。

MongoConnection connection = new MongoConnection();
MongoDBClientConnection clientConnection = new MongoDBClientConnection(){MongoDBClient = new MongoClient( MongoClientSettings settings = new MongoClientSettings
            {
                Server = new MongoServerAddress(localhost, 5477)                
            };)}        
connection.MongoDBServer = clientConnection.MongoDBClient.GetServer();
connection.MongoDBServer.Connect();

以上代码将使用 SSH 隧道连接到 MongoDB 服务器。

【讨论】:

猜你喜欢
  • 2019-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-24
  • 1970-01-01
  • 2012-07-09
  • 1970-01-01
相关资源
最近更新 更多