Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing nslookup shell output with C#

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.

like image 782
Yes - that Jake. Avatar asked Dec 09 '08 17:12

Yes - that Jake.


1 Answers

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. 
like image 164
mmx Avatar answered Sep 23 '22 18:09

mmx