首页 > Windows开发 > 详细

C#远程执行Linux系统中Shell命令和SFTP上传文件

时间:2016-11-23 23:39:56      阅读:1020      评论:0      收藏:0      [点我收藏+]

一、工具:SSH.Net

  网址:https://github.com/sshnet/SSH.NET

  技术分享

 

二、调用命令代码:

  

  Renci.SshNet.SshClient ssh = new Renci.SshNet.SshClient("192.168.1.104", "sindrol", "123456");
            ssh.Connect();
            while (true)
            {
                Console.WriteLine("please input command:");
                var line = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(line)) continue;
                var cmd = ssh.RunCommand(line);
                if (!string.IsNullOrWhiteSpace(cmd.Error))
                    Console.WriteLine(cmd.Error);
                else
                    Console.WriteLine(cmd.Result);
            }
            ssh.Disconnect();

 

 

三、上传文件代码:

    Renci.SshNet.SftpClient sftp = new Renci.SshNet.SftpClient("192.168.1.104", "sindrol", "123456");
            sftp.Connect();
            FileInfo fi = new FileInfo(@"D:\Project\Matlab\rcnn.rar");
            var allLength = fi.Length;
            sftp.UploadFile(new System.IO.FileStream(fi.FullName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite), "/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);
            }

 

四、用法:

  1、一般用于将一个本地上写好的bash角本,通过Sftp上传到服务器,然后执行运行命令。

  2、由于是C#程序实现的,所以可以清松实现多台批量操作。

C#远程执行Linux系统中Shell命令和SFTP上传文件

原文:http://www.cnblogs.com/songxingzhu/p/6095422.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!