【发布时间】: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 代码中得到它吗?