简体   繁体   中英

running cmd in c# answer yes to prompt

I'm running the below command from c#. There is a prompt that will be shown that I want to answer "yes" to how can I do this with the current code

If I run this as a batch script I can just do

echo y | pscp.exe -batch -pw password E:\\Certs\\client.conf me@<ip>:/home/user

which works - but unsure how I can replicate this using the below

string pscpPath="-batch -pw password E:\\Certs\\client.conf me@<ip>:/home/user";

ExecuteCopyCerts("pscp.exe", pscpPath);

Function:

public Boolean ExecuteCopyCerts(string fileName, string arguments)
{
    txtLiveHubStatus.Text = "";

    try
    {
        System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(fileName, arguments);
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.UseShellExecute = false;
        procStartInfo.CreateNoWindow = true;
        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.StartInfo = procStartInfo;
        proc.Start();
        string result = proc.StandardOutput.ReadToEnd();

        return proc.ExitCode == 0;         
    }
}

Set RedirectStandardInput to true

procStartInfo.RedirectStandardInput = true

and then write to StandardInput

proc.StandardInput.WriteLine("yes");

To reiterate what Hesam said though the prompt is Y, not yes. This is the prompt for the cert, which only occurs on the first call to each new linux machine. I use this code today in one of our applications.

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "pscp";
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = false;
psi.Arguments = $"-r -p -pw {passWord} \"{localFileNamePath}\" {userName}@{hostName}:{remotePath}";
psi.UseShellExecute = false;
psi.CreateNoWindow = true;

using (Process process = new Process())
{
    process.StartInfo = psi;
    process.Start();
    process.StandardInput.WriteLine("Y");
    process.WaitForExit();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM