C# SharpSsh Private-public a pair key to Upload and Download File

先说说背景

公司要我做一个小的Console应用程序,完成每天的文件上传。要上传的服务器是第三方Linux系统,用的是SSH,公钥秘钥配对的方式。


问题步骤

1.生成公钥私钥,将公钥发给要上传到的第三方服务器

2.用Tamir.SharpSsh 完成上传(StackOverflow看了很多英文,找到的)


公钥秘钥的生成,有很多工具,按照第三方的建议,我使用 puttygen.exe 

官方网址:http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

这个putty工具类挺多的,我们只需要生成公钥和私钥,因此只需要下载  puttygen.exe

下载后打开,操作:



很有意思,鼠标在进度条来回画圈,生成的随机码质量就很好




Putty公钥秘钥生成的文档说明很多,我就不多说,简单的截图示意。


第一步骤已经完成,接下来第二步,找第三方类库,找呀找,看了很多英文,C# 公钥秘钥的ssh,牛 ,那就是  Tamir.SharpSsh

像沙漠里的一片淡水湖,开始开采使用。

Nuget 项目引用,注意

DiffieHellman.local,Org.Mentalis.Security 是 Tamir.SharpSSH 依赖的类库,不引用它们,你会兜一圈,别问我为什么,问 Tamir,SharpSSH 的开发人


引用完后,开始使用,开工,我遇到的 问题,花了一天的时间研究,原来是文件版本问题,你们肯定也会遇到


Tamir.SharpSSH 的私钥需要 Open ssh key的格式,所以我们需要转换 Conversions => Export SSH key ,明白?



好了,开工了。什么不说,光贴代码

写了一个Helpe

 public class SSHFTP
    {

        private int _port;
        private string _privateSshKeyPath;
        private Sftp _tamirSftp;


        public SSHFTP(string host, string username, string password)
        {
            this._tamirSftp = new Sftp(host, username, password);
            this._port = 22;
        }

        public SSHFTP(string host, int port, string username, string password)
        {
            this._tamirSftp = new Sftp(host, username, password);
            this._port = port;  
        }

        public SSHFTP(string host, int port, string username, string password, string privateSshKeyPath)
        {
            this._tamirSftp = new Sftp(host, username, password);
            this._port = port;
            this._privateSshKeyPath = privateSshKeyPath;
        }

        private void Connect()
        {
            if (!string.IsNullOrEmpty(this._privateSshKeyPath))
            {
                this._tamirSftp.AddIdentityFile(this._privateSshKeyPath);
            }
            this._tamirSftp.Connect(this._port);
        }

        public bool UploadFiles(string[] files, string toRemotePath)
        {
            if (!this._tamirSftp.Connected)
            {
                this.Connect();
            }

            try
            {
                this._tamirSftp.Put(files, toRemotePath);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                this._tamirSftp.Close();
            }
        }

        public bool UploadFile(string file, string toRemotePath)
        {
            if (!this._tamirSftp.Connected)
            {
                this.Connect();
            }

            try
            {
                this._tamirSftp.Put(file, toRemotePath);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                this._tamirSftp.Close();
            }
        }

        public bool DownloadFiles(string[] files, string toLocalPath)
        {
            if (!this._tamirSftp.Connected)
            {
                this.Connect();
            }

            try
            {
                this._tamirSftp.Get(files, toLocalPath);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                this._tamirSftp.Close();
            }
        }

        public bool DownloadFile(string file, string toLocalPath)
        {
            if (!this._tamirSftp.Connected)
            {
                this.Connect();
            }

            try
            {
                this._tamirSftp.Get(file, toLocalPath);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                this._tamirSftp.Close();
            }
        }

        public string[] ListFiles(string remotePath)
        {
            if (!this._tamirSftp.Connected)
            {
                this.Connect();
            }

            string[] files = new string[] { };
            try
            {
                files = this._tamirSftp.GetFileList(remotePath).Cast<string>().ToArray();
            }
            catch (Exception ex)
            {
            }
            finally
            {
                this._tamirSftp.Close();
            }
            return files;
        }

    }


接下来就测试调用:

        #region 目录配置

        private static string targetDirectory = "D://WomplyDailyTransactoinFiles//";
        private static string privateKeyDirectory = "D://WomplyDailyTransactoinFiles//ssh//";
        private static string privateKeyPath = privateKeyDirectory+"Omega_keys_private.openssh.key";         //本地ssh key的路径
        private static string womplyRemoteMerchantDirectory = "to_womply_test/merchant_action_report/";  //服务器要上传或下载的相对目录
     
        #endregion

 private void WomplyUpload(string localFilePath, string remoteFilePath)
        {
            try
            {
                string host = "sftp.womply.com";
                int port = 22;
                string user = "90909090909";   //第三方服务器给你的UserName
                string pass = "";                        //有了private key 就可以不用密码了
                SSHFTP sftp = new SSHFTP(host, port, user, pass, privateKeyPath);
                bool done = sftp.UploadFile(localFilePath, remoteFilePath);
            }
            catch (Exception ex)
            {
                throw new Exception("WomplyUpload Method " + ex.Message);
            }
           
        }

     注意路径:


 测试方法怎么写都可以

需要源码的过来下:看在我认真的情况,给赏一分撒。

http://download.csdn.net/detail/haiyangzhibing/9640967






评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值