Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send input to the console as if the user is typing?

This is my problem. I have a program that has to run in a TTY, cygwin provides this TTY. When I redirect stdIn the program fails because it does not have a TTY. I cannot modify this program, and need some way of automating it.

How can I grab the cmd.exe window and send it data and make it think the user is typing it?

I'm using C#, I believe there is a way to do it with java.awt.Robot but I have to use C# for other reasons.

like image 751
Malfist Avatar asked Jan 16 '09 17:01

Malfist


People also ask

How do you take user input from console?

Getting user input from the browser console To ask for user input from the browser, you need to use the prompt() method provided by the browser. The prompt() method allows you to accept user input as a string and store it on a variable as follows: const input = prompt();


1 Answers

I have figured out how to send the input to the console. I used what Jon Skeet said. I am not 100% sure this is the correct way to implement this.

If there are any comments on to make this better I would love to here. I did this just to see if I could figure it out.

Here is the program I stared that waited for input form the user

class Program
{
    static void Main(string[] args)
    {
        // This is needed to wait for the other process to wire up.
        System.Threading.Thread.Sleep(2000);

        Console.WriteLine("Enter Pharse: ");

        string pharse = Console.ReadLine();

        Console.WriteLine("The password is '{0}'", pharse);


        Console.WriteLine("Press any key to exit. . .");
        string lastLine = Console.ReadLine();

        Console.WriteLine("Last Line is: '{0}'", lastLine);
    }
}

This is the console app writing to the other one

class Program
{
    static void Main(string[] args)
    {
        // Find the path of the Console to start
        string readFilePath = System.IO.Path.GetFullPath(@"..\..\..\ReadingConsole\bin\Debug\ReadingConsole.exe");

        ProcessStartInfo startInfo = new ProcessStartInfo(readFilePath);

        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardInput = true;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.CreateNoWindow = true;
        startInfo.UseShellExecute = false;

        Process readProcess = new Process();
        readProcess.StartInfo = startInfo;

        // This is the key to send data to the server that I found
        readProcess.OutputDataReceived += new DataReceivedEventHandler(readProcess_OutputDataReceived);

        // Start the process
        readProcess.Start();

        readProcess.BeginOutputReadLine();

        // Wait for other process to spin up
        System.Threading.Thread.Sleep(5000);

        // Send Hello World
        readProcess.StandardInput.WriteLine("Hello World");

        readProcess.StandardInput.WriteLine("Exit");

        readProcess.WaitForExit();
    }

    static void readProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        // Write what was sent in the event
        Console.WriteLine("Data Recieved at {1}: {0}", e.Data, DateTime.UtcNow.Ticks);
    }
}
like image 144
David Basarab Avatar answered Oct 23 '22 03:10

David Basarab