. Net uses SSH. NET to access Linux Hosts through SSH, and ssh. netssh

Source: Internet
Author: User

. Net uses SSH. NET to access Linux Hosts through SSH, and ssh. netssh

The SSH. NET Library is used to add the reference dll to the project. The following code shows how to click the button and connect SSH to the Linux host to run the command and return the command execution result.

 1 protected void btnExcute_Click(object sender, EventArgs e) 2 { 3     string server = ServerAddr.Text; 4     string username = UserName.Text; 5     string password = Password.Text; 6     string command = Command.Text; 7  8     StringBuilder result = new StringBuilder(); 9     try10     {11         using (var client = new SshClient(server, username, password))12         {13             try14             {15                 client.Connect();16                 result.Append(client.RunCommand(command).Execute() + "\r\n");17                 QAResult.Text = result.ToString();18                 client.Disconnect();19             }20             catch (Exception e1)21             {22                 result.Append(e1.Message + "\r\n");23             }24         }25     }26     catch (Exception e1)27     {28         result.Append(e1.Message + "\r\n");29         QAResult.Text = result.ToString();30     }31 }

 

The following code Copies files from a local machine to a Linux host

1 private void TestTranferFiles () 2 {3 string server = ServerAddr. text; 4 string username = UserName. text; 5 string password = Password. text; 6 string command = Command. text; 7 var scp = new ScpClient (server, username, password); 8 scp. connect (); 9 scp. upload (new DirectoryInfo (@ "d: \ aaaa"), "/home/zy"); // Upload all files under d: \ aaaa to 10 scp. disconnect (); 11}

 

Related Article

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.