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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM