Timeout establishing SSH sessions

Apr 21, 2014 at 10:09 PM
I'm using the SSH-Sessions module in Powershell to issue commands to various devices over SSH sessions however I have a few devices that 'hang' when establishing a session or shortly thereafter, requiring that the powershell session be closed.

Does anyone know of a way to specify a timeout on SSH.NET calls? If necessary I could re-write the script as a C# executable although I would prefer to keep this as a Powershell script if possible.

Thanks.
Coordinator
Apr 22, 2014 at 5:42 AM
Jim,

What version of SSH.NET are you using ?
Could you try using the latest beta ?

A timeout can be specified on two levels. For estabilishing the connection, the timeout can be specified using the ConnectionInfo.Timeout property. The default value is 30 seconds.

For all other operations, the timeout can be set using the OperationTimeout property on the individual client classes (eg. SftpClient, SshClient).

Hope this helps,
Gert
Apr 22, 2014 at 12:27 PM
im getting:-

Invoke-SshCommand : Exception calling "RunCommand" with "1" argument(s): "Session operation has timed out"

with any commands im trying to execute

using the latest beta

Gregor
Coordinator
Apr 23, 2014 at 4:46 PM
Gregor/Jim,
Can any of you provide a small repro ?
Preferrable one that I can run again a public server, but I'll take whatever I can get :p
Apr 23, 2014 at 9:47 PM
I'm still trying to determine exactly where the code is hanging. I've added some debug code but since then I haven't run in to the issue.
I'm running into this issue on a certain SSH accessible management devices on my network so there is nothing that would be publicly accessible. Fortunately we are gradually phasing these devices out but I'll have to deal with them for at least another year before they are all gone.
Apr 24, 2014 at 10:44 AM
Hi Jim

The code is very simple, I can connect using ssh but when I execute a command I get the following error:-

Enter-SshSession : Exception calling "RunCommand" with "1" argument(s): "Session operation has timed out"

Code below:-

New-SshSession -ComputerName <myipaddress> -Username 'myservername\username' -Password 'mypassword' -port 22
Enter-SshSession -ComputerName <myipaddress>

try{
$results = Invoke-SshCommand -Command 'mkdir c:\test' -ComputerName <myipaddress>
}
catch {
Write-host $($Error[0].Exception.Message)
}
finally
{
Remove-SshSession -RemoveAll
Get-SshSession
}
$results = Invoke-SshCommand -Command
Coordinator
Apr 24, 2014 at 6:25 PM
Can you provide the full stack trace (instead of just Exception.Message) ?
Modifying this:

Write-host $($Error[0].Exception.Message)

into this should do the trick:

Write-host $($Error[0].Exception)

Thanks!
Apr 25, 2014 at 7:53 AM
Stack Trace below:-

System.Management.Automation.MethodInvocationException: Exception calling "RunCommand" with "1" argument(s): "Session operation has timed out" ---> Renci.SshNet.Common.SshOperationTimeoutException: Sessio
n operation has timed out
at Renci.SshNet.Session.WaitHandle(WaitHandle waitHandle)
at Renci.SshNet.SshCommand.BeginExecute(AsyncCallback callback, Object state)
at Renci.SshNet.SshClient.RunCommand(String commandText)
at CallSite.Target(Closure , CallSite , Object , Object )
--- End of inner exception stack trace ---
at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
at System.Management.Automation.Interpreter.ActionCallInstruction2.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
at System.Management.Automation.PSScriptCmdlet.RunClause(Action
1 clause, Object dollarUnderbar, Object inputToProcess)
at System.Management.Automation.CommandProcessorBase.Complete()