Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NET端口转发问题_C#_.net_Mongodb_Ssh_Ssh Tunnel - Fatal编程技术网

C# .NET端口转发问题

C# .NET端口转发问题,c#,.net,mongodb,ssh,ssh-tunnel,C#,.net,Mongodb,Ssh,Ssh Tunnel,我尝试在应用程序中创建ssh tunel,并从CLI访问远程mongo 远程主机上侦听端口9901的ssh服务器 远程Mongo端口:27017 本地主机有:Windows 10,与2019相比 汇编Renci.SshNet,版本=2016.1.0.0 远程主机是Debian9.9 如果从Cygwin进行端口转发,则访问成功,但如果从C#进行端口转发,则访问失败 情景1 开放端口转发: ssh -p 9901 -L 50001:localhost:27017 user@host.net 从本地

我尝试在应用程序中创建ssh tunel,并从CLI访问远程mongo

远程主机上侦听端口9901的ssh服务器
远程Mongo端口:27017
本地主机有:Windows 10,与2019相比
汇编Renci.SshNet,版本=2016.1.0.0
远程主机是Debian9.9

如果从Cygwin进行端口转发,则访问成功,但如果从C#进行端口转发,则访问失败

情景1

开放端口转发:

ssh -p 9901 -L 50001:localhost:27017 user@host.net
从本地主机连接Mongo:

c:\Program Files\MongoDB\Server\3.6\bin>mongo --port 50001 --host localhost
MongoDB shell version v3.6.13
connecting to: mongodb://localhost:50001/?gssapiServiceName=mongodb
WARNING: No implicit session: Logical Sessions are only supported on server versions 3.6 and greater.
Implicit session: dummy session
MongoDB server version: 3.2.11
WARNING: shell and server versions do not match
>
场景2

端口转发代码:

namespace VT_lic
{
    public static class PortForwarding
    {        
        static SshClient client;
        static ForwardedPortLocal portFwld;

        public static void Connect ()
        {
            PasswordConnectionInfo conn = new PasswordConnectionInfo(
                "host.net",
                9901,
                "user",
                "password"
            );

            client = new SshClient(conn);
            portFwld = new ForwardedPortLocal("localhost", 50001, "host.net", 27017);

            try
            {
                Console.WriteLine("Trying SSH connection...");
                client.Connect();
                Console.WriteLine(client.CreateCommand("ls").Execute());

                if (!client.IsConnected)
                    throw new Exception("Kosha ssh connection failed");       

                client.AddForwardedPort(portFwld);
                portFwld.Start();

                if (!portFwld.IsStarted)
                    throw new Exception("Kosha port forward failed");

            }
            catch (System.Net.Sockets.SocketException ex1)
            {
                throw new Exception("Kosha port forward failed");
            }
        }
    }
}
控制台输出:

备份
mk.sh
温度
文件1
文件2

与Mongo的连接:

c:\Program Files\MongoDB\Server\3.6\bin>mongo --port 50001 --host localhost
MongoDB shell version v3.6.13
connecting to: mongodb://localhost:50001/?gssapiServiceName=mongodb
Netstat:

C:\WINDOWS\system32>netstat -a -n | find "50001"
  TCP    127.0.0.1:58045        127.0.0.1:50001        SYN_SENT
  TCP    [::1]:50001            [::]:0                 LISTENING
我尝试了所有的选择:

new ForwardedPortLocal("localhost", 50001, "host.net", 27017);
new ForwardedPortLocal("localhost", 50001, "localhost", 27017);
new ForwardedPortLocal(50001, "localhost", 27017);
new ForwardedPortLocal(27017, "localhost", 50001);
new ForwardedPortDynamic("localhost", 27017); // --port 27017 in CLI also

您的代码应该如下所示

string boundIP = "127.0.0.1";
string boundPort = "5477";

PasswordConnectionInfo connection = new PasswordConnectionInfo(server_config.Get("server_ip"),Convert.ToInt32(server_config.Get("ssh_port")),server_config.Get("ssh_user"), server_config.Get("ssh_password"));            
    SshClient client = new SshClient(connection);
    client.Connect();        
    //forwardIP = 127.0.0.1 
    // hostIP = 127.0.0.1 because I assume you want to access db which is running on 127.0.0.1
    ForwardedPortLocal forwardedPortLocal = new ForwardedPortLocal(forwardIP, Convert.ToUInt32(forwardPort), "127.0.0.1", Convert.ToUInt32(server_config.Get("database_port")));
    client.AddForwardedPort(forwardedPortLocal);
    forwardedPortLocal.Start();

我假设您不应该在ForwardPortLocal方法中使用“localhost”,请尝试使用“127.0.0.1”。Thanx!!!看起来,该主机需要是ip-127.0.0.1,而不是本地主机。我还使用了Convert.ToUInt32()和4位端口号。