大伟C# SSH类

using System;
using System.IO;
using Renci.SshNet;
using Renci.SshNet.Sftp;

namespace weiCmd
{
    public class WeiSSH
    {
        public static string _host;
        public static string _username;
        public static string _password;
        public static int _port = 22;
        public static string _command;

        public static string _serverdir = "/tmp/";
        public static string _clientdir = @"D:\";
        public static string _filename = "测试表.csv";



        public static ConnectionInfo WeiCreateConnectionInfo(string _host, int _port, string _username)
        {
            //安装凭据和服务器信息
            ConnectionInfo WeiConnInfo = new ConnectionInfo(_host, _port, _username,
             new AuthenticationMethod[]{
                // Pasword based Authentication
                new PasswordAuthenticationMethod(_username,_password),
                 // Key Based Authentication (using keys in OpenSSH Format)
                 //new PrivateKeyAuthenticationMethod("username",new PrivateKeyFile[]{
                 //    new PrivateKeyFile(@"..\openssh.key","passphrase")
                 //}),
             }
         );
            return WeiConnInfo;
        }

        /// <summary>
        /// 不依赖ConnectionInfo直接try连接并执行命令
        /// </summary>
        public void WeiStart()
        {
            using (var client = new SshClient(_host, _username, _password))
            {
                try
                {
                    string command = _command;
                    client.Connect();
                    string result = client.RunCommand(command).Execute();
                    Console.WriteLine(result);
                    client.Disconnect();
                }
                catch (Exception e1)
                {
                    Console.WriteLine(e1.Message);
                }
            }

        }

        /// <summary>
        /// 执行 command
        /// </summary>
        public static void WeiExecuteCommand()
        {
            var WeiConnInfo = WeiCreateConnectionInfo(_host, _port, _username);
            using (var sshclient = new SshClient(WeiConnInfo))
            {
                sshclient.Connect();
                using (var cmd = sshclient.CreateCommand(_command))
                {
                    string result = cmd.Execute();
                    Console.WriteLine("Command>" + cmd.CommandText);
                    Console.WriteLine("Return Value = {0}", cmd.ExitStatus);
                    Console.WriteLine("result>" + result);
                }
                sshclient.Disconnect();
            }
        }


        /// <summary>
        /// 上传
        /// </summary>
        public static void WeiUpload()
        {
            var WeiConnInfo = WeiCreateConnectionInfo(_host, _port, _username);
            using (var sftp = new SftpClient(WeiConnInfo))
            {
                string uploadfn = _clientdir + _filename;

                sftp.Connect();
                sftp.ChangeDirectory(_serverdir);
                using (var uplfileStream = System.IO.File.OpenRead(uploadfn))
                {
                    sftp.UploadFile(uplfileStream, _filename, true);
                }
                sftp.Disconnect();
            }
        }

        /// <summary>
        /// 下载
        /// </summary>
        public static void WeiDownload()
        {
            var WeiConnInfo = WeiCreateConnectionInfo(_host, _port, _username);
            using (var sftp = new SftpClient(WeiConnInfo))
            {
                string downloadfn = _serverdir + _filename;
                sftp.Connect();

                string destinationPath = string.Format(@"{0}\{1}", _clientdir, _filename);
                //读取server的SftpFileStream文件流
                using (SftpFileStream sftpStream = sftp.OpenRead(downloadfn))
                {
                    if (sftpStream.Length > 0)
                    {
                        //定义写入本地的FileStream文件流
                        FileStream fileStream = File.Create(destinationPath, (int)sftpStream.Length);
                        //定义buffer大小200K
                        byte[] buffer = new byte[200 * 1024];
                        int count;
                        //每次读取的buffer是200K,读取的偏移量是0,实际读取的长度
                        //每次吧实际读取的长度赋值给count
                        //如果count<=0,终止读取
                        //同时终止写入
                        //偏移量0说明每次流读或流写的时候,读过或写过的就自动默认跳过了
                        while ((count = sftpStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            fileStream.Write(buffer, 0, count);
                        }
                    }
                }
            }
        }
    }


    /// <summary>
    /// WeiSSH类使用示例
    /// </summary>
    public class WeiSSHUse
    {
        public static void WeiSSHFuncUse()
        {
            WeiSSH._host = "1.1.1.8";
            WeiSSH._port = 22;
            WeiSSH._username = "root";
            WeiSSH._password = "830";
            WeiSSH._command = "cd /tmp/; ls -lrt";
            WeiSSH.WeiExecuteCommand();

            WeiSSH._serverdir = "/tmp/";
            WeiSSH._clientdir = @"D:\";
            WeiSSH._filename = "测试表.csv";
            WeiSSH.WeiUpload();

            WeiSSH._command = "mv /tmp/测试表.csv /tmp/测试表linux.csv";
            WeiSSH.WeiExecuteCommand();

            WeiSSH._serverdir = "/tmp/";
            WeiSSH._clientdir = @"D:\";
            WeiSSH._filename = "测试表linux.csv";
            WeiSSH.WeiDownload();
        }
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值