2008-12-09 12 views
6

के साथ nslookup खोल आउटपुट कैप्चर करना मेरे पास एक कमांड लाइन प्रक्रिया है जिसे मैं स्वचालित रूप से सी # में स्वचालित और कैप्चर करना चाहता हूं।सी #

कमांड लाइन में, मैं टाइप करें:

nslookup 

यह एक खोल जो मुझे एक> शीघ्र देता लॉन्च किया गया। प्रॉम्प्ट पर, मैं तो टाइप करें:

ls -a mydomain.local 

यह मेरा प्राथमिक DNS सर्वर और शारीरिक मशीनों वे से जुड़े होते हैं से स्थानीय CNAME उपलब्ध की एक सूची देता है।

मैं क्या करना चाहता हूं यह प्रक्रिया सी # से स्वचालित है। यदि यह एक साधारण कमांड था, तो मैं केवल Process.StartInfo.RedirectStandardOutput = true का उपयोग करता हूं, लेकिन दूसरे चरण की आवश्यकता मुझे ट्राइप कर रही है।

उत्तर

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. 
+0

यह निश्चित रूप से मुझे सही दिशा में मिल गया। मैं इस काम को विज्ञापन के रूप में बनाने के लिए एसिंक्रोनस रीड और इवेंट्स का उपयोग कर समाप्त हुआ क्योंकि मानकऑटपुट। रीड टॉन्ड() हर बार जम गया। .WaitForExit()। –

+0

हाँ, निश्चित रूप से वह हिस्सा थोड़ा मुश्किल था। मैंने इसे आपके पास छोड़ दिया क्योंकि मुझे वास्तव में पता नहीं था कि आप आउटपुट का उपयोग कैसे करना चाहते हैं। –

+0

एसिंक्रोनस पढ़ने के संबंध में, यह एक वास्तविक समस्या हो सकती है। मेरे आवेदन में, मैं उस कोड का उपयोग करता हूं जिसे मैंने [ब्लॉग पोस्टिंग] में पाया नमूना से अनुकूलित किया था (http://www.hanselman.com/blog/SoManyMistakesForMeToMakeSoLittleTimecapturingStandardErrorAndStandardOutput.aspx)। –

2

आपने जो नहीं पूछा, लेकिन मैंने एक बार एक ऐप लिखा जो आपने किया है। मैं अंततः DNS लुकअप करने के लिए .NET लाइब्रेरी का उपयोग करने के लिए चला गया, जो बहुत तेज़ हो गया।

मुझे यकीन है कि मैंने कोडप्रोजेक्ट साइट से this library का उपयोग किया है।

1

मुझे पता है कि यह एक पुराना है, लेकिन अभी भी योगदान देना पसंद है। मैंने अपने डोमेन में एक computername से IPv4 पते प्राप्त करने के लिए "NsLookup होस्टनाम सर्वर" से खोल आउटपुट का उपयोग किया और DNS सर्वर/ipv6 पते जैसे किसी भी अन्य जानकारी को बाहर निकाला ..

यह थोड़ी जल्दी से किया गया है लेकिन यह काम करता है, यदि कोई विफल विफल रहता है तो आप एक विफलता भी जोड़ते हैं जिसे आप सी # से निर्मित nslookup विधि का उपयोग करेंगे।

यह काफी लंबा है, लेकिन मुझे बाहरी लाइब्रेरी का उपयोग किये बिना या आईएसवी 4 को शैल से पढ़ने की संभावना नहीं मिली है, बिना डीएसएन सर्वर का चयन करने की अनुमति देता है।

यदि आप बीच में लूप के बारे में सोच रहे हैं, तो अधिक सुरुचिपूर्ण समाधान हो सकते हैं लेकिन मेरे व्यक्तिगत उपयोग के लिए, यह काफी अच्छा काम करता है, हमारे डोमेन में अधिकांश मेजबान 2 आईपीवी 6 और 2 आईपीवी 4 लौटाते हैं, इसलिए, यह परीक्षण 4 बार तक।

आशा इस मदद कर सकते हैं ..

private void button1_Click(object sender, EventArgs e) 
    { 
     IPAddress[] ips = NsLookup(computername, dnsserver); 

     txtResult.Text = string.Empty; 
     if (ips != null) 
     { 
      txtResult.Text = ips[0].ToString(); 
      txtResult.Text += Environment.NewLine; 
      if (ips[1] != null) 
      { 
       txtResult.Text += ips[1].ToString(); 
      } 
      else 
      { 

      } 
     } 
     else 
     { 
      txtResult.Text = "No IP found"; 
     } 

    } 



    public IPAddress[] NsLookup(string computername, string domaincontroller) 
    { 

     IPAddress[] ips = new IPAddress[2]; 

     try 
     { 
      // Creating streamreaders to read the output and the errors 
      StreamReader outputReader = null; 
      StreamReader errorReader = null; 

      string nslookup = @"C:\Windows\System32\Nslookup.exe"; 

      try 
      { 
       // Setting process startupinfo 
       ProcessStartInfo processStartInfo = new ProcessStartInfo(nslookup, computername + " " + domaincontroller); 
       processStartInfo.ErrorDialog = false; 
       processStartInfo.UseShellExecute = false; 
       processStartInfo.RedirectStandardError = true; 
       processStartInfo.RedirectStandardInput = true; 
       processStartInfo.RedirectStandardOutput = true; 
       processStartInfo.WindowStyle = ProcessWindowStyle.Minimized; 

       // Starting Process 
       Process process = new Process(); 
       process.StartInfo = processStartInfo; 
       bool processStarted = process.Start(); 

       if (processStarted) 
       { 
        // Catching the output streams 
        outputReader = process.StandardOutput; 
        errorReader = process.StandardError; 

        string errorresult = errorReader.ReadLine(); 

        errorReader.Close(); 


        if (errorresult != null) 
        { 
         // Failure got thrown in NsLookup Streamreading, try build-in Method 
         try 
         { 
          ips = Dns.GetHostAddresses(computername); 
          return ips; 
         } 
         catch 
         { 
          return null; 
         } 
       } 
       else 
       { 
        // Clearing out all the values before the addresses. 
        outputReader.ReadLine(); 
        outputReader.ReadLine(); 
        outputReader.ReadLine(); 
        outputReader.ReadLine(); 

        // Reading and Verifying the first outputline (the address is found after "Addresses: ") - 2 part of the array is taken (after second space) 
        string outputline = outputReader.ReadLine(); 
        string[] outputlineaftersplit = outputline.Split(' '); 
        string ipfortesting = outputlineaftersplit[2].Trim(); 

        if (verifyIP(ipfortesting) != null)  // First entry is ipv4 
        { 
         ips[0] = verifyIP(ipfortesting); 

         outputline = outputReader.ReadLine(); 
         ipfortesting = outputline.Trim(); 

         if (verifyIP(ipfortesting) != null) // First and second entry are ipv4 
         { 
          ips[1] = verifyIP(ipfortesting); 
          return ips; 
         } 
         else 
         { 
          return ips; 
         } 
        } 
        else 
        { 
         outputline = outputReader.ReadLine(); 
         ipfortesting = outputline.Trim(); 

         if (verifyIP(ipfortesting) != null) 
         { 
          ips[0] = verifyIP(ipfortesting); 

          outputline = outputReader.ReadLine(); 
          ipfortesting = outputline.Trim(); 

          if (verifyIP(ipfortesting) != null) 
          { 
           ips[0] = verifyIP(ipfortesting); 

           outputline = outputReader.ReadLine(); 
           ipfortesting = outputline.Trim(); 

           if (verifyIP(ipfortesting) != null) 
           { 
            ips[1] = verifyIP(ipfortesting); 
            return ips; 
           } 
           else 
           { 
            return ips; 
           } 

          } 
          else 
          { 
           return ips; 
          } 
         } 
         else 
         { 
          outputline = outputReader.ReadLine(); 
          ipfortesting = outputline.Trim(); 

          if (verifyIP(ipfortesting) != null) 
          { 

           ips[0] = verifyIP(ipfortesting); 

           outputline = outputReader.ReadToEnd(); 
           ipfortesting = outputline.Trim(); 

           if (verifyIP(ipfortesting) != null) 
           { 
            ips[1] = verifyIP(ipfortesting); 
            return ips; 
           } 
           else 
           { 
            return ips; 
           } 

          } 
          else 
          { 
           ips = null; 
           return ips; 
          } 
         } 
        } 
       } 
       } 

       else 
       { 
        // Failure got thrown in NsLookup Streamreading, try build-in Method 
        try 
        { 
         ips = Dns.GetHostAddresses(computername); 
         return ips; 
        } 
        catch 
        { 
         return null; 
        } 
       } 
      } 
      catch 
      { 
       System.Windows.Forms.MessageBox.Show("ERROR 1"); 
       // Failure got thrown in NsLookup Streamreading, try build-in Method 
       try 
       { 
        ips = Dns.GetHostAddresses(computername); 
        return ips; 
       } 
       catch 
       { 
        return null; 
       } 
      } 
      finally 
      { 
       if (outputReader != null) 
       { 
        outputReader.Close(); 
       } 
      } 
     } 
     catch 
     { 
      System.Windows.Forms.MessageBox.Show("ERROR 2"); 
      // Failure got thrown in NsLookup Streamreading, try build-in Method 
      try 
      { 
       ips = Dns.GetHostAddresses(computername); 
       return ips; 
      } 
      catch 
      { 
       return null; 
      } 
     } 

    } 

    public IPAddress verifyIP(string ipfromreader) 
    { 
     IPAddress ipresult = null; 
     bool isIP = IPAddress.TryParse(ipfromreader, out ipresult); 

     if (isIP && (ipresult.AddressFamily != AddressFamily.InterNetworkV6)) 
     { 
      return ipresult; 
     } 
     else 
     { 
      return null; 
     } 
    } 


} 

}