在C#中反向SSH端口转发

时间:2013-08-24 17:48:48

标签: c# ssh tunneling

在尝试在Visual Studio C#中组合反向SSH解决方案时,我遇到了这篇文章:

.NET SSH Port Forwarding

使用他的代码工作直到我实际启动端口(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();
                }
            }
        }

    }
}

0 个答案:

没有答案