【发布时间】:2017-01-24 21:03:36
【问题描述】:
我编写了以下代码。由于某种原因, StreamWriter / RedirectStandardInput 似乎无法正常工作。我尝试与之交互的 CLI 似乎不接受甚至接收我输入的命令。
还有一些我也不明白的奇怪的事情。我必须使用 While 循环来读取 StandardOutput 的所有行。
process.StandardOutput.ReadToEnd();
什么都不返回。只有 ReadLine(); 似乎被识别。
编辑
我现在还注意到,如果我在任何 process.StandardInput.WriteLine(""); 之后设置断点调用,断点永远不会到达。该过程永远不会返回到我的 C# 代码。
这也发生在我的 process.StandardOutput.ReadLine() 循环之后。当它到达最后一行时,它永远不会返回到我的 c# 代码。
代码如下:
Process process = new Process();
process.StartInfo.FileName = @"C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe";
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();
process.StandardInput.AutoFlush = true;
string line = string.Empty;
while ((line = process.StandardOutput.ReadLine()) != null)
{
Console.WriteLine("CLI Says: " + line);
}
process.StandardInput.WriteLine("connect vpn.service.domain.com");
line = string.Empty;
while ((line = process.StandardOutput.ReadLine()) != null)
{
Console.WriteLine("CLI Says: " + line);
}
process.StandardInput.WriteLine("0");
line = string.Empty;
while ((line = process.StandardOutput.ReadLine()) != null)
{
Console.WriteLine("CLI Says: " + line);
}
process.StandardInput.WriteLine("MyUsername");
line = string.Empty;
while ((line = process.StandardOutput.ReadLine()) != null)
{
Console.WriteLine("CLI Says: " + line);
}
process.StandardInput.WriteLine("MyPassword");
line = string.Empty;
while ((line = process.StandardOutput.ReadLine()) != null)
{
Console.WriteLine("CLI Says: " + line);
}
我在调试控制台中收到的输出是:
CLI Says: Cisco AnyConnect Secure Mobility Client (version 3.1.10010) .
CLI Says:
CLI Says:
CLI Says: Copyright (c) 2004 - 2015 Cisco Systems, Inc. All Rights Reserved.
CLI Says:
CLI Says:
CLI Says:
CLI Says:
CLI Says: >> state: Disconnected
CLI Says:
CLI Says:
CLI Says: VPN>
CLI Says: >> state: Disconnected
CLI Says:
CLI Says:
CLI Says: VPN>
CLI Says: >> notice: Ready to connect.
CLI Says:
CLI Says:
CLI Says: VPN>
CLI Says: >> registered with local VPN subsystem.
CLI Says:
CLI Says:
CLI Says: VPN>
【问题讨论】:
标签: c# asp.net .net command-line-interface cisco