資源簡介
C# 利用Renci庫來實現ssh客戶端,可以執行ssh命令并獲取返回結果。一般的Renci ssh例子都不太好用,不能獲取到命令的執行結果(shell執行命令后的輸出信息),本例子是可以的。通過創建ShellStream,執行命令后,使用Expect方法并配合正則表達式,可以獲取到命令執行后的全部輸出信息。
完整的Vs2008 C#代碼,可編譯和測試。

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?Renci.SshNet;
using?Renci.SshNet.Common;
using?System.Text.Regularexpressions;
namespace?sshCmdTest
{
????class?Program
????{
????????static?void?Main(string[]?args)
????????{
????????????var?sshClient?=?new?SshClient(“192.168.2.108“?22?“root“?“abc.1234“);//主機,端口,用戶名和密碼
????????????try
????????????{
????????????????sshClient.Connect();
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????Console.WriteLine(ex.Message);
????????????????return;
????????????}
????????????ShellStream?shellStreamSSH?=?sshClient.CreateShellStream(“shell“?80?60?800?600?65536);//創建shell
????????????string?hello?=?““;
????????????TimeSpan?span?=?new?TimeSpan(0?0?10);//命令執行的超時時間是10秒,可以跟據需要調整
????????????Regex?r?=?new?Regex(“(\\[root@localhost?~\\]#)+“);//??[root@localhost?~]#?是命令執行結束后的提示符,\\表示轉義,因為在Regex里面[和]有特殊含義
????????????hello?=?shellStreamSSH.Expect(r?span);//獲取登錄后的打印信息,比如類似:Last?login:?Sun?Aug?11?13:43:22?2019?from?192.168.2.124\r\r\n[root@localhost?~]#?
????????????Console.WriteLine(hello);
????????????string?res?=?““;
????????????string?command?=?“ls?/mnt“;
????????????shellStreamSSH.WriteLine(command);//發送命令
????????????shellStreamSSH.Flush();
????????????res?=?shellStreamSSH.Expect(r?span);//等待命令執行結束,如果失敗了就會返回null,正常執行就會返回結果信息
????????????Console.WriteLine(res);
????????????command?=?“ifconfig“;
????????????shellStreamSSH.WriteLine(command);
????????????shellStreamSSH.Flush();
????????????res?=?shellStreamSSH.Expect(r?span);
????????????Console.WriteLine(res);
??
????????????sshClient.Disconnect();
????????????Console.ReadLine();
????????}
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????920??2019-08-08?15:56??sshCmdTest.sln
????..A..H.?????17920??2019-08-11?11:31??sshCmdTest.suo
?????文件?????470016??2019-03-02?16:45??sshCmdTest\bin\Debug\Renci.SshNet.dll
?????文件???????5120??2019-08-11?13:51??sshCmdTest\bin\Debug\sshCmdTest.exe
?????文件??????15872??2019-08-11?13:51??sshCmdTest\bin\Debug\sshCmdTest.pdb
?????文件??????14328??2019-08-11?13:51??sshCmdTest\bin\Debug\sshCmdTest.vshost.exe
?????文件????????490??2009-06-11?05:14??sshCmdTest\bin\Debug\sshCmdTest.vshost.exe.manifest
?????文件?????470016??2019-03-02?16:45??sshCmdTest\dll\Renci.SshNet.dll
?????文件?????167266??2019-03-02?16:49??sshCmdTest\dll\Renci.SshNet.rar
?????文件???????4559??2019-08-08?15:58??sshCmdTest\obj\Debug\ResolveAssemblyReference.cache
?????文件????????366??2019-08-11?13:51??sshCmdTest\obj\Debug\sshCmdTest.csproj.FileListAbsolute.txt
?????文件???????5120??2019-08-11?13:51??sshCmdTest\obj\Debug\sshCmdTest.exe
?????文件??????15872??2019-08-11?13:51??sshCmdTest\obj\Debug\sshCmdTest.pdb
?????文件???????1985??2019-08-11?13:52??sshCmdTest\Program.cs
?????文件???????1370??2019-08-08?15:56??sshCmdTest\Properties\AssemblyInfo.cs
?????文件???????2746??2019-08-08?15:58??sshCmdTest\sshCmdTest.csproj
?????目錄??????????0??2019-08-08?15:56??sshCmdTest\obj\Debug\TempPE
?????目錄??????????0??2019-08-08?15:58??sshCmdTest\bin\Debug
?????目錄??????????0??2019-08-11?13:51??sshCmdTest\obj\Debug
?????目錄??????????0??2019-08-08?15:56??sshCmdTest\bin
?????目錄??????????0??2019-08-08?15:57??sshCmdTest\dll
?????目錄??????????0??2019-08-08?15:56??sshCmdTest\obj
?????目錄??????????0??2019-08-08?15:56??sshCmdTest\Properties
?????目錄??????????0??2019-08-11?13:52??sshCmdTest
-----------?---------??----------?-----??----
??????????????1193966????????????????????24
評論
共有 條評論