I've been using SSH.NET for all my backend needs, but I've recently been stuck at this one problem.
The command I want to execute is designed to continuously run, only to be manually terminated (by Ctrl+C for example), and it periodically spits out some output. My goal is to just grab the first output (or the first few, doesn't matter to me).
Via SSH.NET I haven't found a good way to get this done. I recently tried to use System.Threading.Tasks
to perhaps make the command timeout, but the command execution doesn't care for the timeout and still runs regardless.
This is the code I have that I hoped would have worked:
string Output;
using (var client = new SshClient(SSHHost, 22, SSHUSER, SSHWORD))
{
client.Connect();
SshCommand CmdInstance = client.CreateCommand(Command);
var NonTerm = Task.Run(() => CmdInstance.Execute());
if (NonTerm.Wait(TimeSpan.FromSeconds(timeout)))
Console.Write(NonTerm.Result);
Output = NonTerm.Result;
client.Disconnect();
}
return Output;
While you can implement this using SSH.NET for sure, I believe that both more efficient and easier to implement would be to do this server-side.
See How to Terminate Script after a certain number of output.