• Renci.SshNet.SshClient:用于执行 SSH 命令。 • Renci.SshNet.SftpClient:用于 SFTP 操作。 • Renci.SshNet.ScpClient:用于 SCP 操作。 • Renci.SshNet.PrivateKeyFile:用于处理私钥文件。 • Renci.SshNet.SshCommand:用于执行 SSH 命令。 • Renci.SshNet.ShellStream:用于实现交互式 Shell/Terminal。
SFTP 上传和列出文件
using Renci.SshNet;
using System.IO;
using (var client = new SftpClient("sftp.foo.com", "guest", "pwd"))
{
client.Connect();
using (var fs = File.OpenRead(@"C:\tmp\test-file.txt"))
{
client.UploadFile(fs, "/home/guest/test-file.txt");
}
foreach (varfilein client.ListDirectory("/home/guest/"))
{
Console.WriteLine($"{file.FullName} {file.LastWriteTime}");
}
}
使用 SshClient 类执行 SSH 命令
using Renci.SshNet;
using (var client = new SshClient("sftp.foo.com", "guest", new PrivateKeyFile("path/to/my/key")))
{
client.Connect();
using (var cmd = client.RunCommand("echo 'Hello World!'"))
{
Console.WriteLine(cmd.Result); // 输出 "Hello World!\n"
}
}
这个库适合执行一些远程操作,用windows机器集中管理linux机器。