gpt4 book ai didi

c# - 在 SSH 隧道内创建转发端口

转载 作者:行者123 更新时间:2023-11-29 22:22:21 25 4
gpt4 key购买 nike

我正在尝试使用SSH.NET创建从 localhost:3306 到远程计算机上端口 3306 的隧道:

  PrivateKeyFile file = new PrivateKeyFile(@" .. path to private key .. ");
using (var client = new SshClient(" .. remote server .. ", "ubuntu", file))
{

client.Connect();
var port = new ForwardedPortLocal(3306, "localhost", 3306);
client.AddForwardedPort(port);
port.Start();

// breakpoint set within the code here

client.Disconnect();
}

当断点被击中时,client.IsConnected 返回true,但telnet localhost 3306 未连接。如果我使用 Putty 创建连接,并在那里设置相同的隧道,则会成功。我错过了什么?

最佳答案

通过将 ForwardedPortLocal 的参数更改为:

    var port = new ForwardedPortLocal("localhost", 3306, "localhost", 3306);

(以明确我要绑定(bind)到哪个接口(interface)),并在 port.Start(); 之前添加以下代码:

    port.RequestReceived += delegate(object sender, PortForwardEventArgs e)
{
Console.WriteLine(e.OriginatorHost + ":" + e.OriginatorPort);
};

我注意到输出如下:

    ::1:60309

其中的e.OriginatorHost部分是::1,它相当于localhost的IPv6;但是,目标服务器使用的是 IPv4。将参数更改为:

    var port = new ForwardedPortLocal("127.0.0.1", 3306, "localhost", 3306);

强制隧道通过 IPv4 运行,然后我的代码就完全按照我的预期工作了。

关于c# - 在 SSH 隧道内创建转发端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30596676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com