You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SSH Connection Failure (when setting up a connection for the second time): Session operation has timed out #1049
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()
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.
Activity
SINIKI commentedon Nov 30, 2022
@drieseng @LemonPi314: Could you please guide here?
expyram commentedon 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 commentedon Dec 21, 2023
It's done in #1274
WojciechNagorski commentedon Dec 29, 2023
The 2023.0.1 version has been released to Nuget: https://www.nuget.org/packages/SSH.NET/2023.0.1