Skip to content

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

Closed
@SINIKI

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

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