Ssh.Net.Execute() command is not returning

103 views Asked by At

I am trying to reboot twice a yocto remote device from a C# application with Ssh.Net. The first time everything works well and I can see the remote device rebooting. In between there are various steps that are executed, which should not be relevant to the issue. The second time, the Exec() command is not returning and the C# program gets stuck.

This was working with a previous Yocto distribution on the remote device, so I believe something changed on the Ssh server on the remote device, but I do not know how to adapt the C# application to fix the issue.

public void Reboot()
    {
        SshClient sshClient;
        SshCommand rebootCmd;
        sshClient.Connect();
        string rebootCmdline = "reboot";
        rebootCmd = sshClient.CreateCommand(rebootCmdline);
        try
        {
            rebootCmd.Execute(); //it does not return the second time this code runs
        }
        catch (SshConnectionException) { }
    }

I can send ssh commands to the remote device from the command prompt:

ssh user@host 'reboot'

UPDATE:

I used ShellStream instead of SshCommand and now the server executes the command, but the client does not wait for the response, so the program does not hang:

public void Reboot()
    {
        SshClient sshClient;
        sshClient.Connect();
        ShellStream shellStream = this.sshClient.CreateShellStream(string.Empty, 0, 0, 0, 0, 0);
        shellStream.WriteLine("reboot");
        Thread.Sleep(1000);

        this.SshDisconnect();

I added a sleep between the command and disconnecting to avoid race conditions.

0

There are 0 answers