在Unity3D項目開發工具時須要用到SSH鏈接遠程Linux服務器執行命令,找到SharpSSH連接庫後,經過此方法就可以使用。c#
/// <summary> /// SSH登陸遠程Linux服務器,並運行指令 /// </summary> /// <param name="host">遠程Linux服務器IP或域名</param> /// <param name="username">帳號名</param> /// <param name="password">帳號密碼</param> /// <param name="commands">命令</param> /// <returns></returns> 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; } }