Get the App
SLTechnology News&Howtos  ›  Servers  › 

C # establishes SSH connection with Linux server through SharpSSH library and executes commands

Shulou Source: shulou.com Published: 2022-06-03 04:15:36 09月19日 Update

When developing tools for Unity3D projects, you need to use SSH to connect to the remote Linux server to execute commands. After you find the SharpSSH link library, you can use this method.

/ SSH logs in to the remote Linux server and runs the command / remote Linux server IP or domain name / account name / / account password / / command / public static bool RunSSHCommands (String host, String username, String password, String [] commands) {if (commands = = null | | commands.Length = = 0) return false Try {SshExec exec = new SshExec (host, username); exec.Password = password; / / XXLog.Log (String.Format ("[{0}] Connecting...", host)); exec.Connect (); / / XXLog.Log ("OK") Foreach (String command in commands) {if (command = = null | | command.Trim (). Length = = 0) continue; string output = exec.RunCommand (command); / / XXLog.Log (output);} / / XXLog.Log ("Disconnecting..."); exec.Close () / / XXLog.Log ("OK"); return true;} catch (Exception e) {XXLog.Log (e.Message); return false;}}

Attachment: http://down.51cto.com/data/2366664

Tags: Servers services commands accounts domain names passwords tools development tools instructions this method links attachments projects development login run C# Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MariaDB Shulou Information Docker NVidia