Skip to content

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

Closed

Description

@SINIKI

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()

Activity

  1. SINIKI commented on Nov 30, 2022

    @SINIKI
    Author

    @drieseng @LemonPi314: Could you please guide here?

  2. expyram commented on Mar 3, 2023

    @expyram

    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

  3. WojciechNagorski commented on Dec 21, 2023

    @WojciechNagorski
    Collaborator

    It's done in #1274

  4. added this to the 2023.0.1 milestone on Dec 21, 2023
  5. WojciechNagorski commented on Dec 29, 2023

    @WojciechNagorski
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions