sshnet / SSH.NET

SSH.NET is a Secure Shell (SSH) library for .NET, optimized for parallelism.
http://sshnet.github.io/SSH.NET/
MIT License
3.96k stars 932 forks source link

ssh session disconnected after each command #816

Open w00dl3s opened 3 years ago

w00dl3s commented 3 years ago

Using SSH.NET (2020.0.1) [from nuget] in a .NET Core 5 Web application. A controller triggers the ssh connection in the following way

using (var client = new SshClient(clIP, clUser, clPass))
            {
                //--- Accept Host key
                client.HostKeyReceived += delegate (object sender, HostKeyEventArgs e) { e.CanTrust = true; };
                client.ErrorOccurred += ssh_error;

                //--- Start the connection
                client.Connect();

                //--- Bugtest
                SshCommand sc1 = client.CreateCommand("time");
                var asyncExec1 = sc1.BeginExecute().AsyncWaitHandle.WaitOne(-1);
                string result1 = sc1.Result;
                //result1: "\r\nSystem time is 2021-04-26 14:47:08 Europe/Berlin CEST (UTC+02:00)\r\nDaylight Saving Time: active\r\n"

                //---> client.IsConnected == false !!

                SshCommand sc2 = client.CreateCommand("time");
                var asyncExec2 = sc2.BeginExecute().AsyncWaitHandle.WaitOne(-1);
                string result2 = sc2.Result;
                //result2: 
            }

Exception:

SshConnectionException: An established connection was aborted by the server.
Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout)
Renci.SshNet.Session.Renci.SshNet.ISession.WaitOnHandle(WaitHandle waitHandle)
Renci.SshNet.Channels.ChannelSession.Open()
Renci.SshNet.SshCommand.BeginExecute(AsyncCallback callback, object state)
Renci.SshNet.SshCommand.BeginExecute()
SupLanProtect.Classes.clsClavister.BlockIP(string IpAddress) in clsClavister.cs
+
                var asyncExec2 = sc2.BeginExecute().AsyncWaitHandle.WaitOne(-1);
SupLanProtect.Controllers.BadIpController.Get(string ipaddress) in BadIpController.cs
+
            List<Models.SshCommand> cmd = _clsClavister.BlockIP(ipaddress);
lambda_method23(Closure , object )
Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor+TaskOfActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
System.Threading.Tasks.ValueTask<TResult>.get_Result()
System.Runtime.CompilerServices.ValueTaskAwaiter<TResult>.GetResult()
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask<IActionResult> actionResultValueTask)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
IgorMilavec commented 3 years ago

You are missing a call to EndExecute before accessing Result. Why not just call Execute?