以编程方式输入 cmd "ENTER" 进程中.exe参数



我正在开发一个C#WinForms应用程序,以自动执行在Windows cmd窗口中执行的手动TLS/SSL证书请求。CA提供一个桌面客户端,它与CA的服务器通信;挑战";并进行所有权验证。

基本上它是一个三步过程:

  1. CA问题挑战
  2. 用户在相关域上创建包含质询的DNS TXT记录
  3. CA检查记录;如果发现,将颁发新的SSL证书

质询发布期间,请求中列出的每个域的处理都会暂停,并等待用户按ENTER键。发出最后一个质询后,下一次按下ENTER键时,将开始验证。在CMD窗口中手动运行进程会导致

J:Verify_DNS>le64 --key account.key --email "az@example.com" 
--csr example.csr --csr-key example.key 
--crt example.crt --generate-missing 
--domains "example.com,*.example.com"  
--handle-as dns
[ Crypt::LE64 client v0.38 started. ]
Challenge for example.com requires the following DNS record to be created:
Host: _acme-challenge.example.com, type: TXT, value:  
cb4XNLh_ZnkwkuD7EiwlV9wk7qsP8QHLHUlQ2OO5DX8
Check for DNS propagation using: 
nslookup -q=TXT _acme-challenge.example.com
When you see a text record returned, press <Enter>
Challenge for *.example.com requires the following DNS record to be created:
Host: _acme-challenge.example.com, type: TXT, value: 
nUhw1Xy9H219nfiEx0vZuGDVbpe5KXuUenFoOlc3-4Q
Check for DNS propagation using:
nslookup -q=TXT _acme-challenge.example.com
When you see a text record returned, press <Enter>
Processing the 'dns' verification
Verification result for 'example.com': success.
Verification result for '*.example.com': success.
Saving the full cert chain to example.crt.
The job is done.
J:Verify_DNS>

由于DNS传播时间是不可预测的,该过程可以通过附加";延迟;标志。拆分时,流程的第一步在发出质询后停止,并将控制权返回给用户。

代码运行步骤一和nslookup成功

ProcessStartInfo si = new ProcessStartInfo();
si.FileName = @"C:WindowsSystem32cmd.exe";
si.RedirectStandardOutput = true;
si.RedirectStandardError = true;
si.RedirectStandardInput = true;
si.UseShellExecute = false;
si.CreateNoWindow = true;
si.WorkingDirectory = Terms.dir_DNS;
si.Verb = "runas";
using (Process proc = new Process())
{
proc.StartInfo = si;
proc.ErrorDataReceived += cmd_DataReceived_DNS;
proc.OutputDataReceived += cmd_DataReceived_DNS;
proc.EnableRaisingEvents = true;
proc.Start();
proc.PriorityClass = ProcessPriorityClass.RealTime;
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
if (nslookup) proc.StandardInput.WriteLine(@"ipconfig /flushdns");
proc.StandardInput.WriteLine(arg); 
proc.StandardInput.WriteLine("exit");
proc.WaitForExit();
}

在第二步中,整个过程从顶部开始运行,伴随着等待用户按ENTER键的暂停。为了使过程自动化,我需要";"模拟/仿真";在cmd窗口中按ENTER键。

这个问题似乎是一个阻碍;任何想法都将不胜感激。

[EDIT]删除了通过SendKeys 不适当地尝试输入{ENTER}的令人困惑的失败尝试

只需在不带任何参数的情况下调用proc.StandardInput.WriteLine(),因为在控制台窗口中按enter键只是向stdin写入一个行终止符,通常是rn(Windows(或n(*nix(,也称为CR/LF

由于DNS传播时间是不可预测的,该过程可以通过添加";延迟;标志。

理想的方法是等待所需的输出并继续执行。在这种情况下,您可能希望使用BeginOutputReadLine():同步读取输出

string output = null;
// Replace "completed_message" with the termination message you want, maybe "The job is done" here.
while(!(output = proc.StandardOutput.ReadLine()).Contains("completed_message"))
{
ProcessOutput(output);
}
// continue execution or exit the process here

最新更新