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

posted @ 2022-03-09 19:10  Lulus  阅读(1280)  评论(0编辑  收藏  举报