zoukankan      html  css  js  c++  java
  • c# 使用ssh.net 上传文件

      在ssh.net 客户端实例下无法普通用户切换到su root  超级用户,原因是tty 的不支持,具体原因未查, 连接时用超级用户,问题解决

    使用ssh.net  能实现远程命令,  使用其中的sftp 文件传输类,也可实现上传下载

    sftp连接

       Renci.SshNet.SshClient ssh;
            Renci.SshNet.SftpClient sftp;
            public void SftpConnect(string addr, int port, string user,string pass)
            {
                 sftp = new Renci.SshNet.SftpClient(addr,port,user, pass);
            }

    sftp上传

      public void UploadData(string filename,string linuxfilepath)
            {            
                sftp.Connect();
                FileInfo fi = new FileInfo(filename);
                var allLength = fi.Length;
                sftp.UploadFile(new System.IO.FileStream(fi.FullName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite), linuxfilepath/* "/home/sindrol/aa.zip"*/, (pro) => { Console.WriteLine((pro * 1.0d / allLength * 1.0d).ToString("P")); });
                Console.WriteLine("finished.");
                while (true)
                {
                    System.Threading.Thread.Sleep(500);
                }
            }

    注意:报错failtrue

    UploadFile 函数中 文件目录是包含文件名的目录,不是文件夹目录linuxfilepath 像这个 /* "/home/sindrol/aa.zip"*/ 

    加了异常处理的方法
      public bool SftpConnect(string addr, int port, string user,string pass,out string mess)
            {
                bool result = false;
                try
                {
                    //  using (sftp = new Renci.SshNet.SftpClient(addr, port, user, pass))
                    sftp = new Renci.SshNet.SftpClient(addr, port, user, pass);
                        sftp.Connect();
                        mess = "连接成功";
                        return result = true;
                }
                catch (Exception ex)
                {
                    mess = "连接失败,错误:"+ ex.Message;
                    return result = false;
                }
            }
  • 相关阅读:
    引用传递函数值
    关于引用(python中的伪指针)的理解
    学生管理系统_排序后通过name删除列表里的字典
    学生管理系统(函数版)
    全局变量和局部变量的理解
    lambda隐藏函数的嵌套
    lambda函数常见用法
    函数的多个返回值
    函数的函数名重名
    函数的嵌套
  • 原文地址:https://www.cnblogs.com/zuochanzi/p/8744924.html
Copyright © 2011-2022 走看看