C # Connectivity Operations Linux

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.