代码之家  ›  专栏  ›  技术社区  ›  KDE

Ssh.Net.Execute()命令未返回

  •  0
  • KDE  · 技术社区  · 3 年前

    我试图用Ssh.Net从C#应用程序重新启动两次yocto远程设备。第一次一切正常,我可以看到远程设备正在重新启动。在两者之间,执行了各种步骤,这些步骤不应与问题相关。第二次,Exec()命令没有返回,C#程序被卡住了。

    这是在远程设备上使用以前的Yocto发行版,所以我相信远程设备上的Ssh服务器发生了变化,但我不知道如何调整C#应用程序来解决这个问题。

    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) { }
        }
    

    我可以从命令提示符向远程设备发送ssh命令:

    ssh user@host 'reboot'

    更新:

    我使用ShellStream而不是SshCommand,现在服务器执行该命令,但客户端不等待响应,因此程序不会挂起:

    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();
    

    我在命令和断开连接之间增加了一个睡眠,以避免比赛条件。

    0 回复  |  直到 3 年前