开发手册 欢迎您!
软件开发者资料库

.NET Core通过SSH.NET实现sftp通过ssh上传下载文件代码

本文主要介绍.NET Core使用SSH.NET框架实现ssh sftp 上传下载文件。SSH.NET是用于.NET的Secure Shell(SSH-2)库,针对并行性进行了优化。

1、SSH.NET安装引用

1)使用Nuget命令安装

Install-Package SSH.NET -Version 2016.0.0

2)使用Nuget管理工具搜索"SSH.NET"=>找到选择安装

相关文档VS(Visual Studio)中Nuget的使用

2)使用.Net CLI安装

dotnet add package SSH.NET --version 2016.0.0

2、使用密码和公钥验证建立SFTP连接

var connectionInfo = new ConnectionInfo("sftp.foo.com",
"guest",
new PasswordAuthenticationMethod("guest", "pwd"),
new PrivateKeyAuthenticationMethod("rsa.key"));
using (var client = new SftpClient(connectionInfo))
{
client.Connect();
}

3、验证主机标识

使用用户名和密码建立SSH连接,如果服务器的指纹与预期的指纹不匹配,则拒绝连接:

byte[] expectedFingerPrint = new byte[] {                                            0x66, 0x31, 0xaf, 0x00, 0x54, 0xb9, 0x87, 0x31,                                            0xff, 0x58, 0x1c, 0x31, 0xb1, 0xa2, 0x4c, 0x6b                                        };using (var client = new SshClient("sftp.foo.com", "guest", "pwd")){    client.HostKeyReceived += (sender, e) =>        {            if (expectedFingerPrint.Length == e.FingerPrint.Length)            {                for (var i = 0; i < expectedFingerPrint.Length; i++)                {                    if (expectedFingerPrint[i] != e.FingerPrint[i])                    {                        e.CanTrust = false;                        break;                    }                }            }            else            {                e.CanTrust = false;            }        };    client.Connect();}

4、使用SSH.NET上传文件

using Renci.SshNet;public static class SendFileToServer{    private static string host = "127.0.0.1";    private static string username = "sftp";    private static string password = "12345";    public static int Upload(string fileName)    {            var connectionInfo = new ConnectionInfo(host, "sftp", new PasswordAuthenticationMethod(username, password));        // Upload File        using (var sftp = new SftpClient(connectionInfo)){            sftp.Connect();            //sftp.ChangeDirectory("/MyFolder");            using (var uplfileStream = System.IO.File.OpenRead(fileName)){                sftp.UploadFile(uplfileStream, fileName, true);            }            sftp.Disconnect();        }        return 0;    }}

5、使用SSH.NET下载文件

using Renci.SshNet;public static class DownloadFileToServer{    private static string host = "127.0.0.1";    private static string username = "sftp";    private static string password = "12345";    public static int Download(string fileName)    {            var connectionInfo = new ConnectionInfo(host, "sftp", new PasswordAuthenticationMethod(username, password));        // Upload File        using (var sftp = new SftpClient(connectionInfo)){            sftp.Connect();            //sftp.ChangeDirectory("/MyFolder");            using (var fileStream = System.IO.File.OpenWrite(fileName)){                sftp.DownloadFile(fileName, fileStream);            }            sftp.Disconnect();        }        return 0;    }}

相关文档 (C#)SSH.NET sftp常用操作工具类代码(连接,上传,下载,删除,移动)