编辑代码

using System;
using Renci.SshNet;

namespace SshExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建一个SSH客户端实例
            using (var client = new SshClient("192.168.109.129", "root", "11185730waq"))
            {
                // 连接到远程服务器
                client.Connect();

                // 检查连接是否成功
                if (client.IsConnected)
                {
                    Console.WriteLine("已成功连接到远程服务器");

                    // 执行一个命令并获取结果
                    var command = client.CreateCommand("ls");
                    var result = command.Execute();
                    Console.WriteLine("命令执行结果:");
                    Console.WriteLine(result);
                }
                else
                {
                    Console.WriteLine("无法连接到远程服务器");
                }

                // 断开与远程服务器的连接
                client.Disconnect();
            }
        }
    }
}















// using System;
// using Renci.SshNet;

// namespace SSHRemoteScript
// {
//     class Program
//     {
//         static void Main(string[] args)
//         {
//             // 设置SSH连接信息
//             var host = "192.168.109.129";
//             var username = "root";
//             var password = "11185730waq";
//             var port = 22; // 默认SSH端口号为22

//             // 创建SSH客户端实例
//             using (var client = new SshClient(host, port, username, password))
//             {
//                 // 连接到远程主机
//                 client.Connect();

//                 // 执行远程命令
//                 var command = client.CreateCommand("your_command");
//                 var result = command.Execute();

//                 // 输出命令执行结果
//                 Console.WriteLine("Command output: " + result);

//                 // 断开连接
//                 client.Disconnect();
//             }
//         }
//     }
// }