【问题标题】:Raspbian shutdown from Windows IoT Core从 Windows IoT Core 关闭 Raspbian
【发布时间】:2019-05-27 00:28:31
【问题描述】:

我正在 Visual Studio 中构建一个 Windows UWP 应用程序,该应用程序在 Raspberry Pi 3B 上运行。从这里我想关闭/重新启动另一个运行 Raspbian 的 Raspberry Pi。 这可能吗?怎么做? 提前谢谢你

【问题讨论】:

    标签: uwp raspbian windows-10-iot-core


    【解决方案1】:

    是的,这是可能的。您可以在 UWP 应用中使用 Chilkat.SshAsmodat.Standard.SSH.NET 等 SSH 客户端连接到 Raspbian,然后发送关机/重启命令(例如sudo shutdown –h now)给它。 Asmodat.Standard.SSH.NET 如果是免费库,但 Chilkat.Ssh 不是。您可以找到一些其他支持 .Net Standard 2.0/UAP 10 的 SSH 库。

    更新:

    以下代码 sn-p 使用 Asmodat.Standard.SSH.NET 库。它适用于我在 Windows IoT Core 上运行的 UWP 应用。

    C#

        private async void ButtonReboot_Click(object sender, RoutedEventArgs e)
        {
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,  async () =>
            {
                using (var client = new SshClient("xxx.xxx.xxx.xxx", "xx", "xxxxxxx"))
                {
                    client.Connect();
                    var command = client.CreateCommand("sudo reboot -f");
                    var asyncResult = command.BeginExecute();
                    var reader = new StreamReader(command.OutputStream);
    
                    while (!asyncResult.IsCompleted)
                    {
                        await reader.ReadToEndAsync();
                    }
    
                    command.EndExecute(asyncResult);
    
                    client.Disconnect();
                }
            });
        }
    

    VB.NET

    Private Async Sub ButtonReboot_Click(sender As Object, e As RoutedEventArgs)
    
        Await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                Async Sub()
                    Using sClient = New SshClient("XXX.XXX.XXX", "XX", "XXXX")
                        sClient.Connect()
                        If sClient.IsConnected Then
                            Dim sCommand = sClient.CreateCommand("sudo reboot -f \r\n")
                            Dim asyncResult = sCommand.BeginExecute()
                            Dim reader = New StreamReader(sCommand.OutputStream)
    
                            While Not asyncResult.IsCompleted
                                Await reader.ReadToEndAsync()
                            End While
    
                            sCommand.EndExecute(asyncResult)
    
                            Debug.WriteLine("Reboot")
                            sClient.Disconnect()
                        End If
                    End Using
                End Sub)
    
    End Sub
    

    【讨论】:

    • 新年快乐。非常感谢您的回复。不幸的是,Chilkat 不是免费的。有不同的解决方案吗?
    • @henrikl2000,您可以使用 Asmodat.Standard.SSH.NET。这是 Renci SSH.NET 库的 .NET Core / Net Standard 2.0 版本。我已经用这个库进行了测试,它可以工作。起初我尝试使用 SSH.NET,我认为它会支持 UAP 10.0,但我收到错误“服务器响应不包含 SSH 协议标识”。
    • @henrikl2000,我已经更新了我的回复,希望对你有帮助。
    • 嗨,Michael,我已经在我的 UWP 代码中测试了 Asmodat.Standard.SSH.NET,它使用 Raspberry Pi 的 IP 地址或名称工作。 Raspberry Pi 使用您的示例重新启动,但我的程序在发送 ssh 命令后挂起。你知道解决办法吗?
    • @henrikl2000,您可以使用异步的BeginExecute 方法代替RunCommand 方法,
    猜你喜欢
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    相关资源
    最近更新 更多