public override ProcessOutput ExecuteCcm(string args, int timeout = 90000, bool throwOnProcessError = true) { var executable = GetExecutable(ref args); Trace.TraceInformation(executable + " " + args); var output = new ProcessOutput(); if (_sshClient == null) { Trace.TraceInformation("Connecting ssh client..."); var kauth = new KeyboardInteractiveAuthenticationMethod(_user); var pauth = new PasswordAuthenticationMethod(_user, _password); var connectionInfo = new ConnectionInfo(_ip, _port, _user, kauth, pauth); kauth.AuthenticationPrompt += delegate(object sender, AuthenticationPromptEventArgs e) { foreach (var prompt in e.Prompts) { if (prompt.Request.ToLowerInvariant().StartsWith("password")) { prompt.Response = _password; } } }; if (!string.IsNullOrEmpty(_privateKeyFilePath)) { var privateKeyAuth = new PrivateKeyAuthenticationMethod(_user, new PrivateKeyFile[] { new PrivateKeyFile(_privateKeyFilePath) }); connectionInfo = new ConnectionInfo(_ip, _port, _user, privateKeyAuth); } _sshClient = new SshClient(connectionInfo); } if (!_sshClient.IsConnected) { _sshClient.Connect(); } var result = _sshClient.RunCommand(string.Format(@"{0} {1}", executable, args)); output.ExitCode = result.ExitStatus; if (result.Error != null) { output.OutputText.Append(result.Error); } else { output.OutputText.Append(result.Result); } if (throwOnProcessError) { ValidateOutput(output); } return(output); }
/// <summary> /// Spawns a new process (platform independent) /// </summary> public static ProcessOutput ExecuteProcess(string processName, string args, int timeout = DefaultCmdTimeout) { var output = new ProcessOutput(); using (var process = new Process()) { process.StartInfo.FileName = processName; process.StartInfo.Arguments = args; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; //Hide the python window if possible process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; #if !NETCORE process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; #endif using (var outputWaitHandle = new AutoResetEvent(false)) using (var errorWaitHandle = new AutoResetEvent(false)) { process.OutputDataReceived += (sender, e) => { if (e.Data == null) { try { outputWaitHandle.Set(); } catch { //probably is already disposed } } else { output.OutputText.AppendLine(e.Data); } }; process.ErrorDataReceived += (sender, e) => { if (e.Data == null) { try { errorWaitHandle.Set(); } catch { //probably is already disposed } } else { output.OutputText.AppendLine(e.Data); } }; process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); if (process.WaitForExit(timeout) && outputWaitHandle.WaitOne(timeout) && errorWaitHandle.WaitOne(timeout)) { // Process completed. output.ExitCode = process.ExitCode; } else { // Timed out. output.ExitCode = -1; } } } return(output); }