views:

1609

answers:

2

I have a command-line process I would like to automate and capture in C#.

At the command line, I type:

nslookup

This launches a shell which gives me a > prompt. At the prompt, I then type:

ls -a mydomain.local

This returns a list of local CNAMEs from my primary DNS server and the physical machines they are attached to.

What I would like to do is automate this process from C#. If this were a simple command, I would just use Process.StartInfo.RedirectStandardOutput = true, but the requirement of a second step is tripping me up.

+3  A: 
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.
Mehrdad Afshari
This definitely got me in the right direction. I ended up using asynchronous reads and events to make this work as advertised because StandardOutput.ReadToEnd() froze every time as did .WaitForExit().
Jekke
Yeah, definitely that part was a little tricky. I left it to you since I didn't really know how you want to use the output.
Mehrdad Afshari
+1  A: 

Not what you asked, but I once wrote an app that did what you're doing. I eventually moved to using a .NET library to do the DNS lookups, which turned out to be a lot faster.

I'm pretty sure I used this library from the CodeProject site.

mackenir