我在 Visual Studio C# 中尝试组合反向 SSH 解决方案时遇到了这篇文章:
使用他的代码一直有效,直到我实际启动端口(port.Start()),然后它抛出一个异常。用 PuTTY 做一个反向隧道工作得很好,所以我相信服务器设置正确。我在实现这个 SSH.NET 库方面做错了吗?
using Renci.SshNet;
using Renci.SshNet.Messages;
using Renci.SshNet.Common;
using System;
using System.Net;
using System.Threading;
namespace rSSHTunnelerSSHNET
{
public class Program
{
public static void Main(String[] arg)
{
String Host = "testserver.remote";
String Username = "testuser";
String Password = "password";
using (var client = new SshClient(Host, Username, Password))
{
client.Connect();
if (client.IsConnected)
{
Console.WriteLine("connected");
var port = new ForwardedPortRemote("targetserver.local", 80, "testserver.remote", 8080);
client.AddForwardedPort(port);
port.Exception += delegate(object sender, ExceptionEventArgs e)
{
Console.WriteLine(e.Exception.ToString());
};
port.Start();
}
else
{
Console.WriteLine("failed to connect.");
Console.ReadLine();
}
}
}
}
}