0
点赞
收藏
分享

微信扫一扫

C# 远程连接Linux执行命令


引用

SSH.NET
string publicIP = "";
int publicPort = 22;
string userName = "root";
string password = "";
string command = "your command";
using (SshClient client = new SshClient(publicIP, publicPort, userName, password))
{
client.Connect();

SshCommand sshCommand = client.CreateCommand(command);


IAsyncResult asyncResult = sshCommand.BeginExecute();

while (!asyncResult.IsCompleted)
await Task.Delay(10);

string result;

if (sshCommand.ExitStatus == 0)
result = sshCommand.Result;
else
result = sshCommand.Error;

Console.WriteLine($"{sshCommand.ExitStatus}:");
Console.WriteLine($"{result}");
}

示例代码

​​ConnectLinuxTestDemo​​

参考资料

​​SSH.NET​​



学习技术最好的文档就是​​【​官方文档​】​​,没有之一。

还有学习资料​​【​Microsoft Learn​】​​、​​【​CSharp Learn​】​​、​​【​My Note​】​​。


举报

相关推荐

0 条评论