使用C#捕获nslookup shell输出

Yes*_*ke. 7 c# shell nslookup .net-2.0

我有一个命令行进程,我想在C#中自动化和捕获.

在命令行中,我键入:

nslookup
Run Code Online (Sandbox Code Playgroud)

这会启动一个shell,它会给我一个>提示符.在提示符下,我输入:

ls -a mydomain.local
Run Code Online (Sandbox Code Playgroud)

这将返回主DNS服务器及其连接的物理机的本地CNAME列表.

我想做的是从C#自动化这个过程.如果这是一个简单的命令,我只会使用Process.StartInfo.RedirectStandardOutput = true,但第二步的要求正在绊倒我.

Meh*_*ari 7

ProcessStartInfo si = new ProcessStartInfo("nslookup");
si.RedirectStandardInput = true;
si.RedirectStandardOutput = true;
Process nslookup = new Process(si);
nslookup.Start();
nslookup.StandardInput.WriteLine("ls -a mydomain.local");
nslookup.StandardInput.Flush();
// use nslookup.StandardOutput stream to read the result. 
Run Code Online (Sandbox Code Playgroud)

  • 在.NET 4中,您需要使用`Process nslookup = Process.Start(si);` (4认同)