在Unity3D项目开发工具时需要用到SSH连接远程Linux服务器执行命令,找到SharpSSH链接库后,通过此方法就可使用。
/// <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; } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。