user4541353
user4541353

Reputation:

get Redirecting output from plink

I am currently trying to write a function that runs plink and captures the output from that process. When I try StandardOutput I get nothing and when I try StandardError I get "Unable to read from standard input: The handle is invalid." I can only assume that this means I am trying to read while plink is waiting for input. I am also passing my command via a text file called apCommands.txt. When I run this command manually it works and gives me the required output, I just can't figure out why I can't redirect that output so I can store it in a results file. Here is the code I have right now.

        string pArgs = "-m \"" + ABSPATH + "\\apCommands.txt\" -ssh myHost -l user -pw password";
        string output;

        if (File.Exists(ABSPATH + "\\apTest.txt"))
            File.Delete(ABSPATH + "\\apTest.txt");
        StreamWriter apTest = new StreamWriter(ABSPATH + "\\apTest.txt");

        Process runAP = new Process();
        ProcessStartInfo apInfoStart = new ProcessStartInfo(ABSPATH + "\\plink.exe");
        apInfoStart.Arguments = pArgs;
        apInfoStart.RedirectStandardOutput = true;
        apInfoStart.CreateNoWindow = true;
        apInfoStart.UseShellExecute = false;
        runAP.StartInfo = apInfoStart;
        runAP.Start();

        output = runAP.StandardOutput.ReadToEnd();
        runAP.WaitForExit();
        apTest.WriteLine("Standard Output");
        apTest.WriteLine(output);
        apTest.Close();
        runAP.Close();

Thanks,

Upvotes: 2

Views: 1647

Answers (1)

simonalexander2005
simonalexander2005

Reputation: 4577

To solve this error, use apInfoStart.RefirectStandardInput = true;

see also: https://social.msdn.microsoft.com/Forums/vstudio/en-US/697324fa-6ce6-4324-971a-61f6eec075be/redirecting-output-from-plink

You can do the same for StandardOutput and StandardError

Upvotes: 1

Related Questions