0. Background
The importance of Linux is now obvious, and learning Linux is a must, and a program to control Linux can also play a big role. For example, we can do an automated deployment of the program to publish the program to Linux above.
1. Add Ssh.net to the project
2. How to use Ssh.net to come directly to the code
Private Static stringHost ="xxx.xxx.xxx.xx"; Private Static stringUsername ="Root"; Private Static stringPassword ="pwd"; //Setup Credentials and Server information Public StaticConnectionInfo Connnfo =NewConnectionInfo (Host, A, username,Newauthenticationmethod[]{//Pasword based Authentication NewPasswordauthenticationmethod (Username,password),//Key Based authentication (using keys in OpenSSH Format)//new Privatekeyauthenticationmethod ("username", new privatekeyfile[]{//New Privatekeyfile (@ ": \openssh.key "," passphrase ")//}), } ); Static voidMain (string[] args) {ExecuteCommand1 (); ExecuteCommand2 (); Upload (); Console.ReadLine (); } /// <summary> ///Start/// </summary> Public voidStart () {using(varClient =Newsshclient (host, username, password)) { Try { stringCommand ="ls"; Client. Connect (); stringresult =client. RunCommand (command). Execute (); Console.WriteLine (result); Client. Disconnect (); } Catch(Exception E1) {Console.WriteLine (E1. Message); } } } /// <summary> ///Execute Command/// </summary> Public Static voidExecuteCommand1 () {using(varSshclient =Newsshclient (Connnfo)) {sshclient. Connect (); using(varcmd = sshclient. CreateCommand ("cd/local && ls")) { stringresult =cmd. Execute (); Console.WriteLine ("command>"+cmd.commandtext); Console.WriteLine ("Return Value = {0}", CMD. Exitstatus); Console.WriteLine ("result>"+result); } sshclient. Disconnect (); } } /// <summary> ///Execute Command/// </summary> Public Static voidExecuteCommand2 () {//Execute (SHELL) Commands using(varSshclient =Newsshclient (Connnfo)) {sshclient. Connect (); //The quick-to-use IST, but the best practice-sshcommand isn't disposed, exitstatus not checked ...Console.WriteLine (sshclient. CreateCommand ("cd/local && Ls-lah"). Execute ()); Console.WriteLine (sshclient. CreateCommand ("pwd"). Execute ()); Console.WriteLine (sshclient. CreateCommand ("cd/local/soft && Ls-lah"). Execute ()); Sshclient. Disconnect (); } } /// <summary> ///Upload/// </summary> Public Static voidUpload () {using(varSFTP =Newsftpclient (Connnfo)) { stringUPLOADFN ="Renci.SshNet.dll"; Sftp.connect (); Sftp.changedirectory ("/local/soft"); using(varUplfilestream =System.IO.File.OpenRead (UPLOADFN)) {Sftp.uploadfile (Uplfilestream, UPLOADFN,true); } sftp.disconnect (); } }
C # Connectivity Operations Linux