C#在单独的DNS服务器上获取远程计算机的IP [英] C# Get IP of Remote Computer on Separate DNS Server

查看:154
本文介绍了C#在单独的DNS服务器上获取远程计算机的IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个实用程序来管理指定的目录根目录中的指定域中的多个计算机(不一定是运行该应用程序的计算机是其成员)。我遇到的问题是一旦我从外部AD OU收集了计算机名称,由于DNS,我无法基于计算机名称进行管理。是否可以在外部DNS服务器上执行DNS查找,只要DNS服务器的IP和该域名的凭据可用?

I am working on a utility for managing multiple computers in a specified domain (not necessarily the domain the computer the application is running on is a member of) within a specified directory root. The problem I am running into is once I have the collection of computer names from the external AD OU, I am unable to manage based on computer name because of DNS. Is it possible to perform DNS lookup on an external DNS server provided the IP of DNS server and credentials for that domain?

这是我用于初始化过程的代码(在同一个域内工作)。真的感谢任何输入!

Here is the code that I have for the initialization process (works within the same domain). Really appreciate any input!

private Task InitializeComputers()
{
    try
    {
        Computers.Clear();
        object cLock = new object();

        PrincipalContext context = new PrincipalContext(ContextType.Domain, CurrentConfiguration.LDAPAddress, 
            CurrentConfiguration.DirectoryRoot, ContextOptions.Negotiate, 
            CurrentConfiguration.AdminUser, CurrentConfiguration.AdminPassword);

        ComputerPrincipal computer = new ComputerPrincipal(context);

        computer.Name = "*";

        PrincipalSearcher searcher = new PrincipalSearcher(computer);

        PrincipalSearchResult<Principal> result = searcher.FindAll();

        Parallel.ForEach(result, (r) =>
        {
            ComputerPrincipal principal = r as ComputerPrincipal;

            DirectoryObject cObject = new DirectoryObject(CurrentConfiguration)
            {
                Name = principal.Name
            };

            lock (cLock)
            {
                Computers.Add(cObject);
            }
        });

    }
    ... // Catch stuff here
}

private async Task InitializeConnections()
{
    Task[] tasks = Computers.Select(x => x.CheckConnectionAsync()).ToArray();
    await Task.WhenAll(tasks);
}

// This is where I need to be able to get the IP Address. Thoughts???
public Task CheckConnectionAsync()
{
    return Task.Run(() =>
    {
        try
        {
            Ping PingCheck = new Ping();
            PingReply Reply = PingCheck.Send(Name); // <--- need IP Address instead

            if (Reply.Status == IPStatus.Success)
            {
                IsAvailable = true;
                IPHostEntry host = Dns.GetHostEntry(Name); // Does not work for machines on different domain.
                IPAddress = host.AddressList.FirstOrDefault().ToString();
            }
            else
            {
                IsAvailable = false;
                IsWMIActive = false;
            }
        }
        ... // catch stuff here ...
    });
}


推荐答案

我发现这是从 Heijden的DNS解析器。我写了一个简单的DnsManager类,使用单个静态GetIPAddress方法从A记录中提取IP。

To follow up, the solution that I found is derived from Heijden's DNS Resolver. I wrote a simple DnsManager class with a single static GetIPAddress method for extracting the IP out of an A Record.

public static string GetIPAddress(string name)
{
    Resolver resolver = new Resolver();
    resolver.DnsServer = ((App)App.Current).CurrentConfiguration.DNSAddress;
    resolver.Recursion = true;
    resolver.Retries = 3;
    resolver.TimeOut = 1;
    resolver.TranportType = TransportType.Udp;

    Response response = resolver.Query(name, QType.A);

    if (response.header.ANCOUNT > 0)
    {
         return ((AnswerRR)response.Answer[0]).RECORD.ToString();
    }

    return null;
}

然后,更新的CheckConnectionAsync方法现在写成如下:

Then, the updated CheckConnectionAsync method is now written like so:

public Task CheckConnectionAsync()
{
    return Task.Run(() =>
    {
        try
        {
             IPAddress = DnsManager.GetIPAddress(Name + "." + CurrentConfig.DomainName);
             ... // check availability by IP rather than name...
        }
        // catch stuff here...
    });
}

这篇关于C#在单独的DNS服务器上获取远程计算机的IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆