none
ProcessStartInfo Class

    Question

  • Hi, 

    I'm login to remote linux machine with the help of putty.

                ProcessStartInfo pInfo = new ProcessStartInfo();
                pInfo.Arguments = "-ssh prateek@10.202.167.34 -pw password";
                pInfo.FileName = @"C:\Users\prateek\Desktop\PUTTY.EXE"; 

     

                Process p = new Process();
                p.StartInfo = pInfo;

               p.Start();

    After this putty session start, then i need to login as superUser(webAdmin) and give password. How can i provide or change both userName and password in  pInfo.Arguments after login as prateek(userName).

    Monday, October 01, 2012 1:18 PM

Answers

All replies

  • Hello, you need to use the StandardInput and StandardOutput streams to read and write content to the process:

    http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardinput(v=vs.100).aspx

    http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput(v=vs.100).aspx

    Hope this helps,

    Miguel.

    Monday, October 01, 2012 1:25 PM
  • Hi Miguel,

    One issue is that, when i give password in seperate line, it is not accepting it, it is still prompting for password in console. 

                

                Process p = new Process();

                p.StartInfo = pInfo;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;

                p.Start();

                StreamWriter myStreamWriter = p.StandardInput;

                

                myStreamWriter.WriteLine("sudo su - webadmin");
                myStreamWriter.WriteLine("password");


                p.WaitForExit();
                p.Close();

     In what way we can provide password with StandardIn?


    Thanks for your help.

    Monday, October 01, 2012 2:20 PM
  • Hello, first you can read the StandardOutput to get the text "Password:", then you known that the application is ready to read the password. After that use the StandardInput to write the password.

    Regards,

    Miguel.

    Monday, October 01, 2012 2:27 PM
  • Hi Miguel,

    The problem is when i set  RedirectStandardOutput to  true, it doesn't execute the process till end, it in between like for example: 

    When code is : 

           {     ProcessStartInfo pInfo = new ProcessStartInfo();
                pInfo.Arguments = "-ssh prateek@10.202.104.20 -pw password";
                pInfo.FileName = @"C:\Users\prateek\Desktop\PLINK.EXE";

                Process p = new Process();
                p.StartInfo = pInfo;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;

                p.Start();

                p.StandardInput.WriteLine("sudo su - webadmin");

                p.WaitForExit();

                p.Close();

               Console.Readkey();

    }

    The Output is :

    But when RedirectStandardOutput to true, i.e., 

    {     ProcessStartInfo pInfo = new ProcessStartInfo();
                pInfo.Arguments = "-ssh prateek@10.202.104.20 -pw password";
                pInfo.FileName = @"C:\Users\prateek\Desktop\PLINK.EXE";

                Process p = new Process();
                p.StartInfo = pInfo;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;

               p.StartInfo.RedirectStandardOutput = true;

                p.Start();

                p.StandardInput.WriteLine("sudo su - webadmin");

                p.WaitForExit();

                p.Close();

               Console.Readkey();

    }

    output : 

    Thats it wait for indefinite time . So, how can i get "Password:" text, when this process is halting in between  for indefinite amount of time?

    Thank you 

    Tuesday, October 02, 2012 4:44 AM
  • Hello, as an example on how to interact with input/output/error in console applications you can use the following code:

    ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
    startInfo.RedirectStandardError = true;
    startInfo.RedirectStandardInput = true;
    startInfo.RedirectStandardOutput = true;
    startInfo.UseShellExecute = false;
    Process process = new Process();
    process.ErrorDataReceived += (sender, e) =>
    {
        Console.WriteLine(e.Data);
    };
    process.OutputDataReceived += (sender, e) =>
    {
        Console.WriteLine(e.Data);
    
        switch (e.Data)
        {
            case "test1":
                {
                    process.StandardInput.WriteLine("echo test2");
                }
                break;
    
            case "test2":
                {
                    process.StandardInput.WriteLine("exit");
                }
                break;
        }
    };
    process.StartInfo = startInfo;
    if (process.Start())
    {
        process.BeginErrorReadLine();
        process.BeginOutputReadLine();
        process.StandardInput.WriteLine("echo test1");
        process.WaitForExit();
    }
    

    Hope this helps you and if you still having doubts let me know.

    Regards,

    Miguel.

    • Marked as answer by PrateekThakur Wednesday, October 03, 2012 5:25 AM
    Tuesday, October 02, 2012 12:52 PM
  • Thanks Miguel...That was really helpful..
    Tuesday, October 02, 2012 4:13 PM