code_junkie

Capturing nslookup shell output with C#

Nov 12th, 2011
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.36 KB | None | 0 0
  1. nslookup
  2.  
  3. ls -a mydomain.local
  4.  
  5. ProcessStartInfo si = new ProcessStartInfo("nslookup");
  6. si.RedirectStandardInput = true;
  7. si.RedirectStandardOutput = true;
  8. Process nslookup = new Process(si);
  9. nslookup.Start();
  10. nslookup.StandardInput.WriteLine("ls -a mydomain.local");
  11. nslookup.StandardInput.Flush();
  12. // use nslookup.StandardOutput stream to read the result.
  13.  
  14. private void button1_Click(object sender, EventArgs e)
  15. {
  16. IPAddress[] ips = NsLookup(computername, dnsserver);
  17.  
  18. txtResult.Text = string.Empty;
  19. if (ips != null)
  20. {
  21. txtResult.Text = ips[0].ToString();
  22. txtResult.Text += Environment.NewLine;
  23. if (ips[1] != null)
  24. {
  25. txtResult.Text += ips[1].ToString();
  26. }
  27. else
  28. {
  29.  
  30. }
  31. }
  32. else
  33. {
  34. txtResult.Text = "No IP found";
  35. }
  36.  
  37. }
  38.  
  39.  
  40.  
  41. public IPAddress[] NsLookup(string computername, string domaincontroller)
  42. {
  43.  
  44. IPAddress[] ips = new IPAddress[2];
  45.  
  46. try
  47. {
  48. // Creating streamreaders to read the output and the errors
  49. StreamReader outputReader = null;
  50. StreamReader errorReader = null;
  51.  
  52. string nslookup = @"C:WindowsSystem32Nslookup.exe";
  53.  
  54. try
  55. {
  56. // Setting process startupinfo
  57. ProcessStartInfo processStartInfo = new ProcessStartInfo(nslookup, computername + " " + domaincontroller);
  58. processStartInfo.ErrorDialog = false;
  59. processStartInfo.UseShellExecute = false;
  60. processStartInfo.RedirectStandardError = true;
  61. processStartInfo.RedirectStandardInput = true;
  62. processStartInfo.RedirectStandardOutput = true;
  63. processStartInfo.WindowStyle = ProcessWindowStyle.Minimized;
  64.  
  65. // Starting Process
  66. Process process = new Process();
  67. process.StartInfo = processStartInfo;
  68. bool processStarted = process.Start();
  69.  
  70. if (processStarted)
  71. {
  72. // Catching the output streams
  73. outputReader = process.StandardOutput;
  74. errorReader = process.StandardError;
  75.  
  76. string errorresult = errorReader.ReadLine();
  77.  
  78. errorReader.Close();
  79.  
  80.  
  81. if (errorresult != null)
  82. {
  83. // Failure got thrown in NsLookup Streamreading, try build-in Method
  84. try
  85. {
  86. ips = Dns.GetHostAddresses(computername);
  87. return ips;
  88. }
  89. catch
  90. {
  91. return null;
  92. }
  93. }
  94. else
  95. {
  96. // Clearing out all the values before the addresses.
  97. outputReader.ReadLine();
  98. outputReader.ReadLine();
  99. outputReader.ReadLine();
  100. outputReader.ReadLine();
  101.  
  102. // Reading and Verifying the first outputline (the address is found after "Addresses: ") - 2 part of the array is taken (after second space)
  103. string outputline = outputReader.ReadLine();
  104. string[] outputlineaftersplit = outputline.Split(' ');
  105. string ipfortesting = outputlineaftersplit[2].Trim();
  106.  
  107. if (verifyIP(ipfortesting) != null) // First entry is ipv4
  108. {
  109. ips[0] = verifyIP(ipfortesting);
  110.  
  111. outputline = outputReader.ReadLine();
  112. ipfortesting = outputline.Trim();
  113.  
  114. if (verifyIP(ipfortesting) != null) // First and second entry are ipv4
  115. {
  116. ips[1] = verifyIP(ipfortesting);
  117. return ips;
  118. }
  119. else
  120. {
  121. return ips;
  122. }
  123. }
  124. else
  125. {
  126. outputline = outputReader.ReadLine();
  127. ipfortesting = outputline.Trim();
  128.  
  129. if (verifyIP(ipfortesting) != null)
  130. {
  131. ips[0] = verifyIP(ipfortesting);
  132.  
  133. outputline = outputReader.ReadLine();
  134. ipfortesting = outputline.Trim();
  135.  
  136. if (verifyIP(ipfortesting) != null)
  137. {
  138. ips[0] = verifyIP(ipfortesting);
  139.  
  140. outputline = outputReader.ReadLine();
  141. ipfortesting = outputline.Trim();
  142.  
  143. if (verifyIP(ipfortesting) != null)
  144. {
  145. ips[1] = verifyIP(ipfortesting);
  146. return ips;
  147. }
  148. else
  149. {
  150. return ips;
  151. }
  152.  
  153. }
  154. else
  155. {
  156. return ips;
  157. }
  158. }
  159. else
  160. {
  161. outputline = outputReader.ReadLine();
  162. ipfortesting = outputline.Trim();
  163.  
  164. if (verifyIP(ipfortesting) != null)
  165. {
  166.  
  167. ips[0] = verifyIP(ipfortesting);
  168.  
  169. outputline = outputReader.ReadToEnd();
  170. ipfortesting = outputline.Trim();
  171.  
  172. if (verifyIP(ipfortesting) != null)
  173. {
  174. ips[1] = verifyIP(ipfortesting);
  175. return ips;
  176. }
  177. else
  178. {
  179. return ips;
  180. }
  181.  
  182. }
  183. else
  184. {
  185. ips = null;
  186. return ips;
  187. }
  188. }
  189. }
  190. }
  191. }
  192.  
  193. else
  194. {
  195. // Failure got thrown in NsLookup Streamreading, try build-in Method
  196. try
  197. {
  198. ips = Dns.GetHostAddresses(computername);
  199. return ips;
  200. }
  201. catch
  202. {
  203. return null;
  204. }
  205. }
  206. }
  207. catch
  208. {
  209. System.Windows.Forms.MessageBox.Show("ERROR 1");
  210. // Failure got thrown in NsLookup Streamreading, try build-in Method
  211. try
  212. {
  213. ips = Dns.GetHostAddresses(computername);
  214. return ips;
  215. }
  216. catch
  217. {
  218. return null;
  219. }
  220. }
  221. finally
  222. {
  223. if (outputReader != null)
  224. {
  225. outputReader.Close();
  226. }
  227. }
  228. }
  229. catch
  230. {
  231. System.Windows.Forms.MessageBox.Show("ERROR 2");
  232. // Failure got thrown in NsLookup Streamreading, try build-in Method
  233. try
  234. {
  235. ips = Dns.GetHostAddresses(computername);
  236. return ips;
  237. }
  238. catch
  239. {
  240. return null;
  241. }
  242. }
  243.  
  244. }
  245.  
  246. public IPAddress verifyIP(string ipfromreader)
  247. {
  248. IPAddress ipresult = null;
  249. bool isIP = IPAddress.TryParse(ipfromreader, out ipresult);
  250.  
  251. if (isIP && (ipresult.AddressFamily != AddressFamily.InterNetworkV6))
  252. {
  253. return ipresult;
  254. }
  255. else
  256. {
  257. return null;
  258. }
  259. }
  260.  
  261.  
  262. }
Add Comment
Please, Sign In to add comment