7

我有一个命令行过程,我想在 C# 中自动化和捕获。

在命令行中,我键入:

nslookup

这会启动一个 shell,它给我一个 > 提示。在提示符下,我然后键入:

ls -a mydomain.local

这将返回来自我的主 DNS 服务器的本地 CNAME 列表以及它们所连接的物理计算机。

我想做的是从 C# 自动化这个过程。如果这是一个简单的命令,我将只使用 Process.StartInfo.RedirectStandardOutput = true,但第二步的要求让我很受挫。

4

3 回答 3

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. 
于 2008-12-09T17:48:23.260 回答
2

不是你问的,但我曾经写过一个应用程序来做你正在做的事情。我最终转而使用 .NET 库进行 DNS 查找,结果证明速度要快得多。

我很确定我使用了 CodeProject 网站上的这个库

于 2008-12-09T18:27:33.557 回答
1

我知道这是一个旧的,但仍然喜欢贡献。我使用“NsLookup Hostname Server”的 shell 输出从我们域中的计算机名中获取 IPv4 地址,并删除任何其他信息,如 DNS 服务器/ipv6 地址。

这有点快,但它有效,如果 shell 失败,您将使用 C# 中的内置 nslookup 方法,还会添加一个故障转移。

它相当长,但它让我可以在不使用外部库或不使用内置 nslookup 函数的情况下从 shell 读取 ipv4,因为它确实允许选择 dns 服务器。

如果您想知道中间的 if 循环,可能会有更优雅的解决方案,但对于我个人而言,效果很好,我们域中的大多数主机返回 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;
        }
    }


}

}

于 2011-07-06T14:14:44.270 回答