public override void StartProcess(string command, string arguments, string workdir) { var cmdl = new StringBuilder(); if (command != null) { cmdl.Append(command.Quote() /* do the escaping for it */); cmdl.Append(" "); } cmdl.Append(arguments /* expecting to be already escaped */); var startinfo = new ConEmuStartInfo(); startinfo.ConsoleProcessCommandLine = cmdl.ToString(); startinfo.ConsoleProcessExtraArgs = " -cur_console:P:\"<Solarized Light>\""; startinfo.StartupDirectory = workdir; startinfo.WhenConsoleProcessExits = WhenConsoleProcessExits.KeepConsoleEmulatorAndShowMessage; startinfo.AnsiStreamChunkReceivedEventSink = (sender, args) => FireDataReceived(new TextEventArgs(args.GetText(GitModule.SystemEncoding))); startinfo.ConsoleProcessExitedEventSink = (sender, args) => { _nLastExitCode = args.ExitCode; FireProcessExited(); }; startinfo.ConsoleEmulatorClosedEventSink = (s, e) => { if (s == _terminal.RunningSession) { FireTerminated(); } }; startinfo.IsEchoingConsoleCommandLine = true; _terminal.Start(startinfo); }
private static Form CreatePingForm() { var form = new Form() { AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink, Padding = new Padding(10), Text = "Ping Command" }; FlowLayoutPanel stack; form.Controls.Add(stack = new FlowLayoutPanel() { Dock = DockStyle.Fill, AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink, FlowDirection = FlowDirection.TopDown }); stack.Controls.Add(new Label() { AutoSize = true, Dock = DockStyle.Top, Text = "Running the ping command.", Padding = new Padding(5) }); Label labelWaitOrResult; stack.Controls.Add(labelWaitOrResult = new Label() { AutoSize = true, Dock = DockStyle.Top, Text = "Please wait…", BackColor = Color.Yellow, Padding = new Padding(5) }); ConEmuControl conemu; var sbText = new StringBuilder(); stack.Controls.Add(conemu = new ConEmuControl() { AutoStartInfo = null, MinimumSize = new Size(800, 600), Dock = DockStyle.Top }); ConEmuSession session = conemu.Start(new ConEmuStartInfo() { AnsiStreamChunkReceivedEventSink = (sender, args) => sbText.Append(args.GetMbcsText()), ConsoleProcessCommandLine = "ping 8.8.8.8", LogLevel = ConEmuStartInfo.LogLevels.Basic }); session.ConsoleProcessExited += delegate { Match match = Regex.Match(sbText.ToString(), @"\(.*\b(?<pc>\d+)%.*?\)", RegexOptions.Multiline); if (!match.Success) { labelWaitOrResult.Text = "Ping execution completed, failed to parse the result."; labelWaitOrResult.BackColor = Color.PaleVioletRed; } else { labelWaitOrResult.Text = $"Ping execution completed, lost {match.Groups["pc"].Value} per cent of packets."; labelWaitOrResult.BackColor = Color.Lime; } }; session.ConsoleEmulatorClosed += delegate { form.Close(); }; return(form); }
public override void StartProcess(string command, string arguments, string workdir, Dictionary <string, string> envVariables) { var cmdl = new StringBuilder(); if (command != null) { cmdl.Append(command.Quote() /* do the escaping for it */); cmdl.Append(" "); } cmdl.Append(arguments /* expecting to be already escaped */); var startinfo = new ConEmuStartInfo(); startinfo.ConsoleProcessCommandLine = cmdl.ToString(); if (AppSettings.ConEmuStyle.ValueOrDefault != "Default") { startinfo.ConsoleProcessExtraArgs = " -new_console:P:\"" + AppSettings.ConEmuStyle.ValueOrDefault + "\""; } startinfo.StartupDirectory = workdir; foreach (var(name, value) in envVariables) { startinfo.SetEnv(name, value); } startinfo.WhenConsoleProcessExits = WhenConsoleProcessExits.KeepConsoleEmulatorAndShowMessage; var outputProcessor = new ConsoleCommandLineOutputProcessor(startinfo.ConsoleProcessCommandLine.Length, FireDataReceived); startinfo.AnsiStreamChunkReceivedEventSink = outputProcessor.AnsiStreamChunkReceived; startinfo.ConsoleProcessExitedEventSink = (sender, args) => { outputProcessor.Flush(); _nLastExitCode = args.ExitCode; FireProcessExited(); }; startinfo.ConsoleEmulatorClosedEventSink = (s, e) => { if (s == _terminal.RunningSession) { FireTerminated(); } }; startinfo.IsEchoingConsoleCommandLine = true; _terminal.Start(startinfo); }
private void setupConEmu() { ConEmuStartInfo startInfo = new ConEmuStartInfo(); startInfo.ConEmuExecutablePath = @"C:\Program Files\ConEmu\ConEmu64.exe"; startInfo.GreetingText = $"ConEmu version [{FileVersionInfo.GetVersionInfo(_conEmuPath).ProductVersion}]"; startInfo.GreetingText += $"ngrok version [{FileVersionInfo.GetVersionInfo(_ngrokPath).ProductVersion}"; startInfo.ConsoleProcessCommandLine = _ngrokPath + " start --none"; _control = new ConEmuControl(); _session = _control.Start(startInfo); pnlBottom.Controls.Add(_control); _control.Dock = DockStyle.Fill; _control.Enabled = false; }
public override void StartProcess(string command, string arguments, string workdir, Dictionary <string, string> envVariables) { var cmdl = new StringBuilder(); if (command != null) { cmdl.Append(command.Quote() /* do the escaping for it */); cmdl.Append(" "); } cmdl.Append(arguments /* expecting to be already escaped */); var startinfo = new ConEmuStartInfo(); startinfo.ConsoleProcessCommandLine = cmdl.ToString(); if (AppSettings.ConEmuStyle.ValueOrDefault != "Default") { startinfo.ConsoleProcessExtraArgs = " -new_console:P:\"" + AppSettings.ConEmuStyle.ValueOrDefault + "\""; } startinfo.StartupDirectory = workdir; foreach (var envVariable in envVariables) { startinfo.SetEnv(envVariable.Key, envVariable.Value); } startinfo.WhenConsoleProcessExits = WhenConsoleProcessExits.KeepConsoleEmulatorAndShowMessage; startinfo.AnsiStreamChunkReceivedEventSink = (sender, args) => { var text = args.GetText(GitModule.SystemEncoding); if (EnvUtils.RunningOnWindows()) { text = text.Replace("\n", Environment.NewLine); } FireDataReceived(new TextEventArgs(text)); }; startinfo.ConsoleProcessExitedEventSink = (sender, args) => { _nLastExitCode = args.ExitCode; FireProcessExited(); }; startinfo.ConsoleEmulatorClosedEventSink = (s, e) => { if (s == _terminal.RunningSession) { FireTerminated(); } }; startinfo.IsEchoingConsoleCommandLine = true; _terminal.Start(startinfo); }
public override void StartProcess(string command, string arguments, string workDir, Dictionary <string, string> envVariables) { ProcessOperation operation = CommandLog.LogProcessStart(command, arguments, workDir); try { var commandLine = new ArgumentBuilder { command.Quote(), arguments }.ToString(); var outputProcessor = new ConsoleCommandLineOutputProcessor(commandLine.Length, FireDataReceived); var startInfo = new ConEmuStartInfo { ConsoleProcessCommandLine = commandLine, IsEchoingConsoleCommandLine = true, WhenConsoleProcessExits = WhenConsoleProcessExits.KeepConsoleEmulatorAndShowMessage, AnsiStreamChunkReceivedEventSink = outputProcessor.AnsiStreamChunkReceived, StartupDirectory = workDir }; foreach (var(name, value) in envVariables) { startInfo.SetEnv(name, value); } startInfo.ConsoleProcessExitedEventSink = (_, args) => { _nLastExitCode = args.ExitCode; operation.LogProcessEnd(_nLastExitCode); outputProcessor.Flush(); FireProcessExited(); }; startInfo.ConsoleEmulatorClosedEventSink = (sender, _) => { if (sender == _terminal.RunningSession) { FireTerminated(); } }; _terminal.Start(startInfo, ThreadHelper.JoinableTaskFactory, AppSettings.ConEmuStyle.Value, AppSettings.ConEmuFontSize.Value); } catch (Exception ex) { operation.LogProcessEnd(ex); throw; } }
public PanelConsole(ConsoleSession session) { InitializeComponent(); AutoSize = true; Text = session.SessionName; //Resize += new System.EventHandler(TermPanel_Resize); Controls.Add(conemu = new ConEmuControl() { AutoStartInfo = null, Dock = DockStyle.Fill }); con_session = conemu.Start(new ConEmuStartInfo() { ConsoleProcessCommandLine = session.CommandLine }); }
public override void StartProcess(string command, string arguments, string workdir) { var cmdl = new CommandLineBuilder(); cmdl.AppendFileNameIfNotNull(command /* do the escaping for it */); cmdl.AppendSwitch(arguments /* expecting to be already escaped */); var startinfo = new ConEmuStartInfo(); startinfo.ConsoleProcessCommandLine = cmdl.ToString(); startinfo.StartupDirectory = workdir; startinfo.WhenConsoleProcessExits = WhenConsoleProcessExits.KeepConsoleEmulatorAndShowMessage; startinfo.AnsiStreamChunkReceivedEventSink = (sender, args) => FireDataReceived(new TextEventArgs(args.GetText(GitModule.SystemEncoding))); startinfo.ConsoleProcessExitedEventSink = (sender, args) => { _nLastExitCode = args.ExitCode; FireProcessExited(); }; startinfo.ConsoleEmulatorClosedEventSink = delegate { FireTerminated(); }; startinfo.IsEchoingConsoleCommandLine = true; _terminal.Start(startinfo); }
private static Form RenderView(Form form) { form.Size = new Size(800, 600); ConEmuControl conemu; form.Controls.Add(conemu = new ConEmuControl() { Dock = DockStyle.Fill, MinimumSize = new Size(200, 200), IsStatusbarVisible = true }); if (conemu.AutoStartInfo != null) { conemu.AutoStartInfo.SetEnv("one", "two"); conemu.AutoStartInfo.SetEnv("geet", "huub"); conemu.AutoStartInfo.GreetingText = "• Running \"cmd.exe\" as the default shell in the terminal. \n\n"; //conemu.AutoStartInfo.GreetingText = "\"C:\\Program Files\\Git\\bin\\git.exe\" fetch --progress \"--all\" "; // A test specimen with advanced quoting conemu.AutoStartInfo.IsEchoingConsoleCommandLine = true; } //conemu.AutoStartInfo = null; TextBox txt; form.Controls.Add(txt = new TextBox() { Text = "AnotherFocusableControl", AutoSize = true, Dock = DockStyle.Top }); FlowLayoutPanel stack; form.Controls.Add(stack = new FlowLayoutPanel() { FlowDirection = FlowDirection.LeftToRight, Dock = DockStyle.Top, AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink }); Button btn; stack.Controls.Add(btn = new Button() { Text = "Paste Command", AutoSize = true, Dock = DockStyle.Left }); btn.Click += delegate { conemu.RunningSession?.WriteInputText("whois microsoft.com" + Environment.NewLine); }; stack.Controls.Add(btn = new Button() { Text = "Write StdOut", AutoSize = true, Dock = DockStyle.Left }); btn.Click += delegate { conemu.RunningSession?.WriteOutputText("\x001B7\x001B[90mEcho \"Hello world!\"\x001B[m\x001B8"); }; stack.Controls.Add(btn = new Button() { Text = "Query HWND", AutoSize = true, Dock = DockStyle.Left }); btn.Click += delegate { conemu.RunningSession?.BeginGuiMacro("GetInfo").WithParam("HWND").ExecuteAsync().ContinueWith(task => txt.Text = $"ConEmu HWND: {Regex.Replace(task.Result.Response, "\\s+", " ")}", TaskScheduler.FromCurrentSynchronizationContext()); }; stack.Controls.Add(btn = new Button() { Text = "Query PID", AutoSize = true, Dock = DockStyle.Left }); btn.Click += delegate { conemu.RunningSession?.BeginGuiMacro("GetInfo").WithParam("PID").ExecuteAsync().ContinueWith(task => txt.Text = $"ConEmu PID: {Regex.Replace(task.Result.Response, "\\s+", " ")}", TaskScheduler.FromCurrentSynchronizationContext()); }; stack.Controls.Add(btn = new Button() { Text = "Kill Payload", AutoSize = true, Dock = DockStyle.Left }); btn.Click += delegate { conemu.RunningSession?.KillConsoleProcessAsync(); }; stack.Controls.Add(btn = new Button() { Text = "Ctrl+C", AutoSize = true, Dock = DockStyle.Left }); btn.Click += delegate { conemu.RunningSession?.SendControlCAsync(); }; CheckBox checkStatusBar; stack.Controls.Add(checkStatusBar = new CheckBox() { Text = "StatusBar", Checked = conemu.IsStatusbarVisible }); checkStatusBar.CheckedChanged += delegate { conemu.IsStatusbarVisible = checkStatusBar.Checked; }; TextBox txtOutput = null; stack.Controls.Add(btn = new Button() { Text = "&Ping", AutoSize = true, Dock = DockStyle.Left }); btn.Click += delegate { if (conemu.IsConsoleEmulatorOpen) { MessageBox.Show(form, "The console is busy right now.", "Ping", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } if (txtOutput == null) { form.Controls.Add(txtOutput = new TextBox() { Multiline = true, Dock = DockStyle.Right, Width = 200 }); } conemu.Start(new ConEmuStartInfo() { ConsoleProcessCommandLine = "ping ya.ru", IsEchoingConsoleCommandLine = true, AnsiStreamChunkReceivedEventSink = (sender, args) => txtOutput.Text += args.GetMbcsText(), WhenConsoleProcessExits = WhenConsoleProcessExits.KeepConsoleEmulatorAndShowMessage, ConsoleProcessExitedEventSink = (sender, args) => txtOutput.Text += $"Exited with ERRORLEVEL {args.ExitCode}.", GreetingText = $"This will showcase getting the command output live in the backend.{Environment.NewLine}As the PING command runs, the textbox would duplicate its stdout in real time.{Environment.NewLine}{Environment.NewLine}" }); }; stack.Controls.Add(btn = new Button() { Text = "&Choice", AutoSize = true, Dock = DockStyle.Left }); btn.Click += delegate { conemu.RunningSession?.CloseConsoleEmulator(); DialogResult result = MessageBox.Show(form, "Keep terminal when payload exits?", "Choice", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (result == DialogResult.Cancel) { return; } ConEmuSession session = conemu.Start(new ConEmuStartInfo() { ConsoleProcessCommandLine = "choice", IsEchoingConsoleCommandLine = true, WhenConsoleProcessExits = result == DialogResult.Yes ? WhenConsoleProcessExits.KeepConsoleEmulatorAndShowMessage : WhenConsoleProcessExits.CloseConsoleEmulator, ConsoleProcessExitedEventSink = (sender, args) => MessageBox.Show($"Your choice is {args.ExitCode} (powered by startinfo event sink).") }); #pragma warning disable 4014 ShowMessageForChoiceAsync(session); #pragma warning restore 4014 }; return(form); }