Sharpssh连接linux上传下载文件

来源:互联网 发布:tensorflow theano 编辑:程序博客网 时间:2024/06/11 01:38
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using System.IO;


using Tamir.SharpSsh;
using Org.Mentalis.Security;
using Tamir.SharpSsh.jsch;


/*
 * 
 * 连接linux,传输数据
 * 
 */
namespace ReplayTool
{
    /// <summary>
    /// Sample showing the use of the SSH file trasfer features of 
    /// SharpSSH such as SFTP and SCP
    /// </summary>
    public class SshFileTransfer
    {
        private Replay replay = null;
        public SshFileTransfer(Replay replay)
        {
            this.replay = replay;
        }
        private void getStruct()
        { 
            
        }
        /*
         * 根据结构体中的信息,上传下载文件
         * 出现异常则返回null
         */
        public string transferFile(ref TransferStructInfo transfer)
        {
            try
            {
               // SshConnectionInfo input = Util.GetInput();
                TransferStructInfo input = new TransferStructInfo { Host = "192.168.50.39", User = "ei", Pass = "ei", Direction = "to", Protocol = "sftp", FromPath = "D:\\c.txt", ToPath = "/home/ei/" };
                input = transfer;
                if (input.Protocol == null)
                {
                    input.Protocol = "sftp";
                }
                string protocol = input.Protocol.ToLower();
                SshTransferProtocolBase sshCp;


                if (protocol.Equals("scp"))
                    sshCp = new Scp(input.Host, input.User);
                else
                    sshCp = new Sftp(input.Host, input.User);


                if (input.Pass != null) sshCp.Password = input.Pass;
                if (input.IdentityFile != null) sshCp.AddIdentityFile(input.IdentityFile);
                sshCp.OnTransferStart += new FileTransferEvent(sshCp_OnTransferStart);
                sshCp.OnTransferProgress += new FileTransferEvent(sshCp_OnTransferProgress);
                sshCp.OnTransferEnd += new FileTransferEvent(sshCp_OnTransferEnd);


                Console.Write("Connecting...");
                sshCp.Connect();
                Console.WriteLine("OK");


                while (true)
                {
                    string direction = input.Direction.ToLower();
                    string fromPath = input.FromPath;
                    string toPath = input.ToPath;
                    if (direction == "to")
                    {
                        sshCp.Put(fromPath, toPath);
                        replay.writeLogToRichTb("【执行信息】上传文件" + input.FromPath + "到用户" + input.User + "的路径" + input.ToPath + "下成功");
                    }
                    else
                    {
                        sshCp.Get(fromPath, toPath);
                    }
                    break;          //此处为临时加,待改动
                }
                sshCp.Close();
                return "ok";
                //replay.writeLogToRichTb("OK");
            }
            catch (Exception e)
            {
                replay.writeLogToRichTb(e.Message);
            }
            return null;
        }


        static Tamir.SharpSsh.jsch.examples.ConsoleProgressBar progressBar;


        private static void sshCp_OnTransferStart(string src, string dst, int transferredBytes, int totalBytes, string message)
        {
            Console.WriteLine();
            progressBar = new Tamir.SharpSsh.jsch.examples.ConsoleProgressBar();
            progressBar.Update(transferredBytes, totalBytes, message);
        }


        private static void sshCp_OnTransferProgress(string src, string dst, int transferredBytes, int totalBytes, string message)
        {
            if (progressBar != null)
            {
                progressBar.Update(transferredBytes, totalBytes, message);
            }
        }


        private static void sshCp_OnTransferEnd(string src, string dst, int transferredBytes, int totalBytes, string message)
        {
            if (progressBar != null)
            {
                progressBar.Update(transferredBytes, totalBytes, message);
                progressBar = null;
            }
        }
    }

//用以存储连接linux所需的信息
    public struct TransferStructInfo
    {
        public string Host; //ip
        public string User; //用户名
        public string Pass; //密码
        public string IdentityFile; //可为空
        public string Direction; //传输方向
        public string Protocol; //协议,可设置为sftp
        public string FromPath; //文件路径
        public string ToPath;
    }
}
0 0
原创粉丝点击