. 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}