none
How to get console terminal output text?

    Question

  • I have looked a lot trying to get this to work. I have a terminal console window of a some program running on old version of linux. How can I get all the text from that window? I tried attaching console to get input I get access_denied message. I tried spy++ to get handle but it wont get me text on nonmanaged process. I tried Gettext() and Sendmessage() neither of those worked. Is there anyway to do this?
    Thanks
    Monday, April 09, 2007 6:40 PM

Answers

  • Depending on what exactly you are looking to do, you could use plink  (a command line version of PuTTY (even can access the stored sessions)) and do something like the following:

    using System;
    using System.IO;
    using System.Diagnostics;

    namespace ScratchProject
    {
        class Program
        {
            static void Main(string[] args)
            {
                Process p = new Process();
                String s;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.FileName = "plink.exe";
                p.StartInfo.Arguments = "@Example -l root -pw password";
                p.Start();
                StreamReader reader = p.StandardOutput;
                StreamWriter writer = p.StandardInput;
                writer.AutoFlush = true;
                writer.Write("ls\n");
                writer.Write("exit\n");
                s = reader.ReadToEnd();
                Console.WriteLine(s);
                Console.ReadLine();
            }

        }
    }



    Monday, April 09, 2007 10:38 PM

All replies

  • Depending on what exactly you are looking to do, you could use plink  (a command line version of PuTTY (even can access the stored sessions)) and do something like the following:

    using System;
    using System.IO;
    using System.Diagnostics;

    namespace ScratchProject
    {
        class Program
        {
            static void Main(string[] args)
            {
                Process p = new Process();
                String s;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.FileName = "plink.exe";
                p.StartInfo.Arguments = "@Example -l root -pw password";
                p.Start();
                StreamReader reader = p.StandardOutput;
                StreamWriter writer = p.StandardInput;
                writer.AutoFlush = true;
                writer.Write("ls\n");
                writer.Write("exit\n");
                s = reader.ReadToEnd();
                Console.WriteLine(s);
                Console.ReadLine();
            }

        }
    }



    Monday, April 09, 2007 10:38 PM
  • Michael,
    This example is exactly what I am trying to do. However, I seem to be stuck at one point. I would like to handle the prompt that comes up when you first connect to a server and move past it. Right now, it seems to be waiting for user entry, then abandons the connection.
    I tried reading the output and sending a "Y" when I read the prompt. However that does not seem to work.

    Any ideas?

    Thanks in Advance,
    Sri J.

    Thursday, October 11, 2007 6:28 PM
  • Hi Michael, I've added a number of helper methods to the O2 Platform (Open Source project) which allow you easily script an interaction with another process via the console output and input (see http://code.google.com/p/o2platform/source/browse/trunk/O2_Scripts/APIs/Windows/CmdExe/CmdExeAPI.cs)

    Also useful for you might be the API that allows the viewing of the console output of the current process (in an existing control or popup window). See this blog post for more details: http://o2platform.wordpress.com/2011/11/26/api_consoleout-cs-inprocess-capture-of-the-console-output/ (this blog also contains details of how to consume the console output of new processes)

    Saturday, November 26, 2011 5:58 PM