Skip to content

SSH Connection Failure (when setting up a connection for the second time): Session operation has timed out #1049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SINIKI opened this issue Nov 22, 2022 · 4 comments
Milestone

Comments

@SINIKI
Copy link

SINIKI commented Nov 22, 2022

Issue:

We have multiple Cisco devices in our system which need to be tested in every 24 hours. For testing, we use a C# application which picks up one device at a time from queue for testing. To connect to these devices, we are using Renci library to setup a SSH connection and run commands on these devices. While SSH connection for the first device completes successfully, the connection keeps failing for the rest of the devices in queue until the application is restarted.

Say, we have 10 devices in Queue:

  • Device 1 => Connection successful
  • Device 2 => Connection fails
  • Device 3 => Connection fails
    ...
    ...
  • Device 10 => Connection fails

Code sample for reference:

private static string h = string.Empty;
private static string u = string.Empty;
private static string p = string.Empty;

public static void RunTest(string DeviceName, string userName, string pswd)
{
    h = DeviceName;
    u = userName;
    p = pswd;
    
    ConnectionInfo ConnInfo = new ConnectionInfo(h, 22, u, new AuthenticationMethod[]
    {
        new PasswordAuthenticationMethod(u, p),
    });
    
    using (var client = new SshClient(ConnInfo))
    {
        client.Connect();
        using (var stream = client.CreateShellStream("", 80, 40, 80, 40, 1024))
        {
            // Run command
            command = "<Command 1>";
            stream.WriteLine(command);
    
            command = "<Command 2>";
            stream.WriteLine(command);
            ...
            command = "<Command 20>";
            stream.WriteLine(command);
    
            // Read Output
            output = stream.Read();
    
            // Print Output
            ...
    
            stream.Dispose();
        }
    
        client.Disconnect();
        client.Dispose();
    }

}

Error Message:

23:11:46 INFO: Renci.SshNet.Common.SshOperationTimeoutException: Session operation has timed out
at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout)
at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle)
at Renci.SshNet.Session.Connect()
at Renci.SshNet.BaseClient.CreateAndConnectSession()
at Renci.SshNet.BaseClient.Connect()

@SINIKI
Copy link
Author

SINIKI commented Nov 30, 2022

@drieseng @LemonPi314: Could you please guide here?

@expyram
Copy link

expyram commented Mar 3, 2023

I experienced exactly this issue when periodically reconnecting to the same Cisco router. The first connection would always work; subsequent connections would (almost) always fail with SshOperationTimeoutException.

#841 solved this issue for me.

Once those commits are in place, you simply set this property, and the connection works reliably:

connectionInfo.LazyIdentification = true;

Many thanks to @likeMyCoffee

@WojciechNagorski
Copy link
Collaborator

It's done in #1274

@WojciechNagorski WojciechNagorski added this to the 2023.0.1 milestone Dec 21, 2023
@WojciechNagorski
Copy link
Collaborator

The 2023.0.1 version has been released to Nuget: https://www.nuget.org/packages/SSH.NET/2023.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants