一、工具: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#程序實現的,所以可以清松實現多台批量操作。