本教程使用的是ssh.net這個庫。項目地址:https://github.com/sshnet/SSH.NET
使用ssh客戶端連接遠程主機執行命令,並拿到輸出結果:
using (var sshClient = new SshClient("host", port,"username", "password"))
{
sshClient.Connect();
using (var cmd = sshClient.CreateCommand("ls -l"))
{
var res = cmd.Execute();
Console.Write(res);
}
}
使用sftp客戶端上傳文件:
using (var sftpClient = new SftpClient("host", port,"username", "password"))
{
sftpClient.Connect();
sftpClient.UploadFile(File.Open(@"D:\index.html", FileMode.Open),"/root/index.html");
}
下載文件:
using (var sftpClient = new SftpClient("host", port,"username", "password"))
{
sftpClient.Connect();
using (var stream = File.Open(@"F:\index.html", FileMode.OpenOrCreate))
{
sftpClient.DownloadFile("/root/index.html", stream);
}
}

