/// <summary> /// Send data to ssh client /// </summary> /// <param name="byteData"></param> private static void SendSSHClientData(byte byteData) { try { shellStream.WriteByte(byteData); shellStream.Flush(); } catch (Exception e) { Trace.TraceError("Failed to send data to ssh client, remote session {0} ({1})", RemoteSessionID, e); throw; } }
/// <summary> /// Send data to ssh client /// </summary> /// <param name="data"></param> private static void SendSSHClientData(byte byteData) { try { shellStream.WriteByte(byteData); shellStream.Flush(); } catch (Exception e) { pipeMessaging.ClosePipes(); DisconnectSSHClient(); } }
public void ShellSend(Gdk.Key key) { if (UserInputMode) { LastKeyPress = key; userkeypress.Set(); } else { if (LocalEcho) { Write(key.ToString()); } if (shellStream != null && shellStream.CanWrite) { shellStream.WriteByte((byte)key); shellStream.Flush(); } } }
public void SSHRunCommand(string command) { StringBuilder reply = new StringBuilder(); ss.Flush(); StreamReader sreader = new StreamReader(ss); sreader.ReadToEnd(); if (command != "\u0003\n") { while (!sreader.EndOfStream) { if (!this.IsDriverRunning) { break; } reply.AppendLine(sreader.ReadLine()); Thread.Sleep(1000); } } if (command.StartsWith("CHAR_")) { command = command.Replace("CHAR_", "").Replace("\n", ""); try { ss.WriteByte(System.Convert.ToByte(Convert.ToInt32(command))); } catch (Exception ex) { Reporter.ToLog(eLogLevel.ERROR, $"Method - {MethodBase.GetCurrentMethod().Name}, Error - {ex.Message}", ex); } } else { ss.Write(command); } try { if (mWait == 0 || mWait == null) { mWait = this.ImplicitWait; } if (mWait == 0) { mWait = 30; } DateTime startingTime = DateTime.Now; bool expOut = false; Regex regExp; mExpString = string.IsNullOrEmpty(mExpString) ? "" : mExpString; if (mExpString != "" && command != "\u0003\n") { regExp = new Regex(@"~~~GINGER_RC_END~~~|" + mExpString); } else { regExp = new Regex(@"\> |\$ |\% |assword: |\. |~~~GINGER_RC_END~~~"); } while ((DateTime.Now - startingTime).TotalSeconds <= mWait && taskFinished != true) { if (!this.IsDriverRunning) { break; } reply.AppendLine(sreader.ReadToEnd()); Thread.Sleep(1000); if (regExp.Matches(reply.ToString()).Count > 0) { if (reply.ToString().Contains(command) && expOut == false) { taskFinished = true; expOut = true; } else { break; } } if (sreader.EndOfStream) { Thread.Sleep(1000); continue; } if ((DateTime.Now - startingTime).TotalSeconds >= mWait) { taskFinished = true; break; } } if (command != "\u0003\n") { mConsoleDriverWindow.ConsoleWriteText(reply.ToString(), true); reply.Clear(); } taskFinished = true; } catch (Exception ex) { Reporter.ToLog(eLogLevel.ERROR, ex.Message); } mConsoleDriverWindow.ConsoleWriteText(reply.ToString(), true); reply.Clear(); }
/// <summary> /// Starts the user interaction. /// </summary> /// <param name="sshClient">The SSH client.</param> /// <param name="sftpClient">The SFTP client.</param> /// <param name="shellStream">The shell stream.</param> /// <param name="verbOptions">The verb options.</param> private static void StartUserInteraction( SshClient sshClient, SftpClient sftpClient, ShellStream shellStream, MonitorVerbOptions verbOptions) { _forwardShellStreamInput = false; while (true) { var readKey = Console.ReadKey(true); if (readKey.Key == ConsoleKey.F1) { _forwardShellStreamInput = !_forwardShellStreamInput; if (_forwardShellStreamInput) { Program.Title = "Monitor (Interactive)"; Terminal.WriteLine(" >> Entered console input forwarding.", ConsoleColor.Green); _forwardShellStreamOutput = true; } else { Program.Title = "Monitor (Press H for Help)"; Terminal.WriteLine(" >> Left console input forwarding.", ConsoleColor.Red); } continue; } if (_forwardShellStreamInput) { if (readKey.Key == ConsoleKey.Enter) { shellStream.Write("\r\n"); } else { shellStream.WriteByte((byte)readKey.KeyChar); } shellStream.Flush(); continue; } switch (readKey.Key) { case ConsoleKey.Q: return; case ConsoleKey.C: Console.Clear(); break; case ConsoleKey.N: CreateNewDeployment(sshClient, sftpClient, shellStream, verbOptions); break; case ConsoleKey.E: RunSshClientCommand(sshClient, verbOptions); break; case ConsoleKey.S: RunShellStreamCommand(shellStream, verbOptions); break; case ConsoleKey.H: const ConsoleColor helpColor = ConsoleColor.Cyan; Terminal.WriteLine("Console help", helpColor); Terminal.WriteLine(" H Prints this screen", helpColor); Terminal.WriteLine(" Q Quits this application", helpColor); Terminal.WriteLine(" C Clears the screen", helpColor); Terminal.WriteLine(" N Force a deployment cycle", helpColor); Terminal.WriteLine(" E Run the Pre-deployment command", helpColor); Terminal.WriteLine(" S Run the Post-deployment command", helpColor); Terminal.WriteLine(" F1 Toggle shell-interactive mode", helpColor); Terminal.WriteLine(); break; default: Terminal.WriteLine($"Unrecognized command '{readKey.KeyChar}' -- Press 'H' to get a list of available commands.", ConsoleColor.Red); break; } } }
/// <summary> /// Starts the user interaction. /// </summary> /// <param name="fsMonitor">The fs monitor.</param> /// <param name="sshClient">The SSH client.</param> /// <param name="sftpClient">The SFTP client.</param> /// <param name="shellStream">The shell stream.</param> /// <param name="verbOptions">The verb options.</param> private static void StartUserInteraction(FileSystemMonitor fsMonitor, SshClient sshClient, SftpClient sftpClient, ShellStream shellStream, MonitorVerbOptions verbOptions) { ForwardShellStreamInput = false; while (true) { var readKey = Console.ReadKey(true); if (readKey.Key == ConsoleKey.F1) { ForwardShellStreamInput = !ForwardShellStreamInput; if (ForwardShellStreamInput) { ConsoleManager.WriteLine(" >> Entered console input forwarding.", ConsoleColor.Green); ForwardShellStreamOutput = true; } else { ConsoleManager.WriteLine(" >> Left console input forwarding.", ConsoleColor.Red); } continue; } if (ForwardShellStreamInput) { if (readKey.Key == ConsoleKey.Enter) { shellStream.WriteLine(string.Empty); } else { shellStream.WriteByte((byte)readKey.KeyChar); } shellStream.Flush(); continue; } if (readKey.Key == ConsoleKey.Q) { break; } if (readKey.Key == ConsoleKey.C) { ConsoleManager.Clear(); continue; } if (readKey.Key == ConsoleKey.N) { CreateNewDeployment(sshClient, sftpClient, shellStream, verbOptions); continue; } if (readKey.Key == ConsoleKey.E) { RunSshClientCommand(sshClient, verbOptions); continue; } if (readKey.Key == ConsoleKey.S) { RunShellStreamCommand(shellStream, verbOptions); continue; } if (readKey.Key != ConsoleKey.H) { ConsoleManager.WriteLine("Unrecognized command '" + readKey.KeyChar + "' -- Press 'H' to get a list of available commands.", ConsoleColor.Red); } if (readKey.Key == ConsoleKey.H) { var helpColor = ConsoleColor.Cyan; ConsoleManager.WriteLine("Console help", helpColor); ConsoleManager.WriteLine(" H Prints this screen", helpColor); ConsoleManager.WriteLine(" Q Quits this application", helpColor); ConsoleManager.WriteLine(" C Clears the screen", helpColor); ConsoleManager.WriteLine(" N Force a deployment cycle", helpColor); ConsoleManager.WriteLine(" E Run the Pre-deployment command", helpColor); ConsoleManager.WriteLine(" S Run the Post-deployment command", helpColor); ConsoleManager.WriteLine(" F1 Toggle shell-interactive mode", helpColor); ConsoleManager.WriteLine(string.Empty); continue; } } }