C# SSH类

先引用Renci.SshNet   https://github.com/sshnet/SSH.NET

另一个例子 https://www.cnblogs.com/DerekDeng/p/12126567.html

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

namespace SSHTest
{
    public class SshHelper
    {
        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 CreateConnectionInfo(string _host, int _port, string _username)
        {
            //安装凭据和服务器信息
            ConnectionInfo WeiConnInfo = new ConnectionInfo(_host, _port, _username, new PasswordAuthenticationMethod(_username,_password));
            return WeiConnInfo;
        }

        /// <summary>
        /// 不依赖ConnectionInfo直接try连接并执行命令
        /// </summary>
        public void Start()
        {
            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 ExecuteCommand()
        {
            var WeiConnInfo = CreateConnectionInfo(_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 Upload()
        {
            var WeiConnInfo = CreateConnectionInfo(_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 Download()
        {
            var WeiConnInfo = CreateConnectionInfo(_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 SSHUse
    {
        public static void WeiSSHFuncUse()
        {
            SshHelper._host = "1.1.1.8";
            SshHelper._port = 22;
            SshHelper._username = "root";
            SshHelper._password = "830";

            SshHelper._command = "cd /tmp/; ls -lrt";
            SshHelper.ExecuteCommand();

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

            SshHelper._command = "mv /tmp/测试表.csv /tmp/测试表linux.csv";
            SshHelper.ExecuteCommand();

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

 

posted @ 2021-11-29 21:15  simadi  阅读(1073)  评论(0编辑  收藏  举报