C# 在SSH隧道中创建转发端口
我正在尝试使用创建从C# 在SSH隧道中创建转发端口,c#,ssh,portforwarding,ssh.net,C#,Ssh,Portforwarding,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 Forw
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);
(以明确我绑定到哪个接口),并在port.Start()之前添加以下代码代码>:
我注意到输出了以下内容:
::1:60309
其中的e.OriginatorHost
部分是::1
,它是与本地主机相当的IPv6;但是,目标服务器正在使用IPv4。将参数更改为:
var port = new ForwardedPortLocal("127.0.0.1", 3306, "localhost", 3306);
强制隧道运行IPv4,然后我的代码完全按照我的预期工作。在遇到同样的问题并对其进行分析后,考虑到这是一个bug,我得出了结论(尽管这可能被认为是有争议的,但显然这种行为让SSH.NET API的用户感到惊讶)
所以我就报了
在修复之前,解决方法一直在起作用。您是否收到针对telnet应用程序的特定警告?关门了吗?二进制连接数据可能无法显示…它挂起,无法连接。当使用Putty时,我会得到一组数据。试试看:我认为断开客户端是个问题。如果您在Start
和Disconnect
之间中断,您也可能会停止转发。在旧版本的库中似乎也有一些错误,请确保您使用的是最新版本。@owlstead谢谢-我已经检查过了,我正在使用最新版本2013.4.7.0。非常感谢您提供的解决方案。我从几小时后就开始尝试解决这个问题。如果您像我一样在您的开发机器上使用本地MySQL实例,请查看我的问题:-)
var port = new ForwardedPortLocal("127.0.0.1", 3306, "localhost", 3306);