/// <summary> /// Will Execute the given command on the given SshSession, and /// print out the COMMAND, [STDOUT], and [STDERR] to console /// or print an error if connection cannot be made /// </summary> /// <param name="sshSess">SSH Session to send command to</param> /// <param name="cmd">string command to execute</param> /// <returns></returns> public static bool ExecuteSshCmdWithFullConsoleOutput(ISshCmd cmd, string cmdInput = null) { cmd.ExecuteCmd(cmdInput); if (cmd.IsExecuted) { Console.WriteLine("COMMAND: \"{0}\"", cmd.CmdText); if (cmd.StdOutText.Length > 0) { Console.WriteLine("[STDOUT]"); Console.WriteLine(cmd.StdOutText); } if (cmd.StdErrText.Length > 0) { Console.WriteLine("[STDERR]"); Console.WriteLine(cmd.StdErrText); } return(true); } else { Console.WriteLine("Unable to Execute Command:"); Console.WriteLine(cmd); return(false); } }
/// <summary> /// Execute a single command on the SSH Connection /// The text command comes from the cmd.CmdText property /// cmd.StdOutText is the command output from StdOut (empty string if none) /// cmd.StdErrText is the command output from StdErr (empty string if none) /// </summary> /// <param name="cmd">an object of ISshCmd type</param> public void ExecuteBaseSingleCommand(ISshCmd cmd) { if (Connect()) { var cmdobj = _ssh.CreateCommand(cmd.CmdText); cmd.StdOutText = cmdobj.Execute(); var reader = new StreamReader(cmdobj.ExtendedOutputStream); cmd.StdErrText = reader.ReadToEnd(); cmd.IsExecuted = true; } else { cmd.StdOutText = null; cmd.StdErrText = null; cmd.IsExecuted = false; } }