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