سؤال

ولدي عملية سطر الأوامر أود أن أتمتة والقبض في C #.

في سطر الأوامر، وأنا اكتب:

nslookup

وهذا يطلق قذيفة الذي يعطيني> موجه. في موجه الأوامر، I ثم اكتب:

ls -a mydomain.local

وهذا ما يعيد قائمة سجلات CNAME المحلية من خدمة بلدي DNS الابتدائي والآلات المادية وترد لهم.

ما أود القيام به هو أتمتة هذه العملية من C #. إذا كان هذا أمر بسيط، وأود أن مجرد استخدام Process.StartInfo.RedirectStandardOutput = صحيح، ولكن شرط الخطوة الثانية هي تنطلق لي.

هل كانت مفيدة؟

المحلول

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. 

نصائح أخرى

وليس ما يطلب منك، لكنني مرة واحدة كتب أحد التطبيقات التي فعلت ما تفعلونه. أنا في نهاية المطاف انتقل الى استخدام مكتبة .NET لقيام عمليات البحث DNS، والتي تحولت إلى أن تكون على نحو أسرع كثيرا.

وأنا متأكد من أنني استخدمت هذه المكتبة من موقع CodeProject .

وأعرف أن هذا هو واحد من العمر، ولكن لا تزال ترغب في المساهمة. اعتدت إخراج قذيفة من "خادم NSLOOKUP اسم المضيف" للحصول على عناوين IPv4 من اسم الكمبيوتر في مجال دينا وتجريد أي معلومات أخرى مثل عناوين ملقم / الإصدار IPv6 DNS ..

ووكيندا بسرعة القيام بذلك ولكنه يعمل، وهناك أيضا فشل أضاف إذا فشلت قذيفة التي ستستخدم المدمج في طريقة NSLOOKUP من C #.

وأنها طويلة نوعا ما ولكنه أعطاني إمكانية قراءة IPv4 من قذيفة دون استخدام مكتبة خارجية أو بدون استخدام المدمج في وظيفة NSLOOKUP كما أنها لا تسمح لاختيار خادم نظام أسماء النطاقات.

إذا كنت تتساءل عن إذا حلقات في الوسط، قد يكون هناك حلول أكثر أناقة ولكن للاستعمال الشخصي، وهذا عملت بشكل جيد جدا، ومعظم الجنود في نطاق لدينا عاد 2 IPv6 و 2 من IPv4، وبالتالي، فإنه اختبار ما يصل إلى 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;
        }
    }


}

و}

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top