Quantcast
Channel: sshnet Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1729

New Post: How to detect a tunnel disconnect?

$
0
0
Hello,

I have this piece of code which establish a remote Port an a Linux mashine:
namespace SSHTest
{
    using System;
    using System.Threading;

    using Renci.SshNet;
    using Renci.SshNet.Common;

    using SSHTest.Properties;

    class Program
    {
        static void Main(string[] args)
        {
            Thread thread1 = new Thread(A);
            thread1.Start();

            Console.WriteLine("Run SSH Thread");
            Console.ReadKey();
        }

        private static void A()
        {
            try
            {
                var ci = new ConnectionInfo("192.168.206.133", 22, "dominik", new PasswordAuthenticationMethod("dominik", Resources.passwd));

                using (var client = new SshClient(ci))
                {
                    client.Connect();
                    client.KeepAliveInterval = new TimeSpan(0, 0, 0, 5);
                    client.ErrorOccurred += client_ErrorOccurred;
                    var fport = new ForwardedPortRemote("192.168.206.133", 8013, "172.24.224.125", 22);
                    client.AddForwardedPort(fport);
                    fport.Start();
                    fport.Exception += fPort_Exception;
                    Console.WriteLine(fport.IsStarted ? "port ok" : "port failed");
                    while (client.IsConnected)
                    {
                        Thread.Sleep(200);
                    }
                }

                Console.WriteLine("Thread Ended !");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.WriteLine("Thread Ended Error!");
            }
        }

        static void client_ErrorOccurred(object sender, ExceptionEventArgs e)
        {
            Console.WriteLine(e.Exception.Message);
        }

        static void fPort_Exception(object sender, ExceptionEventArgs e)
        {
            Console.WriteLine(e.Exception.Message);
        }
    }
}
It is only for testing and the code is not the best.
Anyway ... If I disconnect the Linux machine from the network nothing happens. Same problem is I just disable the network card. It seems that the tunnel still exist.

So how can I detect that the SSH tunnel is broken / disconnected?

I would have expected that I got some kind of exception. But nothing happend.

So is there a general mistake in my code? Or can´t ssh.net detect a broken ssh tunnel?

Any help would be much appreciated.

Dominik

Viewing all articles
Browse latest Browse all 1729

Trending Articles