C# 如何在MongoDB C中连接SSH.NET#

C# 如何在MongoDB C中连接SSH.NET#,c#,mongodb,ssh,ssh.net,C#,Mongodb,Ssh,Ssh.net,我正在使用这个SSH.NET客户端和MongoDB C#最新的客户端。根据,我正在尝试将SSHStream注入MongoDB客户端设置。我的SSH连接成功。但是我无法通过SSH建立到MongoDB的连接 static void Main(string[] args) { MongoClientSettings settings = new MongoClientSettings { ClusterConfigurator =

我正在使用这个SSH.NET客户端和MongoDB C#最新的客户端。根据,我正在尝试将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;
    }
}

非常感谢您的帮助。

您可以使用以下代码启动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客户端将本地主机端口5477转发到远程MongoDB数据库服务器

MongoClient可用于与主机为localhost、端口为5477的MongoDB服务器通信

请参阅下面的代码片段以连接MongoDB服务器

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服务器连接。

1)您能否使用与C代码运行在同一台机器上的任何SSH客户端连接到
hostname:port
?2) 您是否有任何理由相信
CreateShellStream
应该能够与MongoDB合作?我在Jira链接中没有找到任何证据。@MartinPrikryl是的,我能够使用这个
主机:端口连接到SSH服务器。不,他们没有提到任何关于
CreateShellStream
。我不确定它是否能正常工作,但我只看到这个选项将sshclient转换为要传递到
IStreamFactory
的流,因此首先创建一个简单的控制台应用程序,该应用程序只允许ssh传递到
hostname:port
。如果它没有连接,请向我们显示来自SSH客户端的日志文件,以便进行比较。-您眼前的问题与MongoDB无关另外,您没有确认在与C代码相同的计算机上运行SSH客户端。-顺便问一下,你是如何运行你的C#btw的?@MartinPrikryl我对SSH连接没有问题。我的SSH托管在AmazonEC2和
cli.Connect()中创建成功的连接。您所指的SSH客户端是什么?我的C#控制台(SSH.NET)是我使用的客户端。为了确认SSH通信,我使用RoboMongo作为附加SSH客户端,在我的C#代码所在的同一台机器上,并建立了一个通信。那么,“连接被拒绝异常”从何而来?不是在SSH代码中?你能在MongoDB代码中找到它吗?这很有效!!但它并没有像这里所说的那样遵循MongoClient内部的SSH流工厂实现。对此有何解释?