【发布时间】:2019-05-27 00:28:31
【问题描述】:
我正在 Visual Studio 中构建一个 Windows UWP 应用程序,该应用程序在 Raspberry Pi 3B 上运行。从这里我想关闭/重新启动另一个运行 Raspbian 的 Raspberry Pi。 这可能吗?怎么做? 提前谢谢你
【问题讨论】:
标签: uwp raspbian windows-10-iot-core
我正在 Visual Studio 中构建一个 Windows UWP 应用程序,该应用程序在 Raspberry Pi 3B 上运行。从这里我想关闭/重新启动另一个运行 Raspbian 的 Raspberry Pi。 这可能吗?怎么做? 提前谢谢你
【问题讨论】:
标签: uwp raspbian windows-10-iot-core
是的,这是可能的。您可以在 UWP 应用中使用 Chilkat.Ssh 或 Asmodat.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
【讨论】:
BeginExecute 方法代替RunCommand 方法,