【问题标题】:C# Get IP of Remote Computer on Separate DNS ServerC# 在单独的 DNS 服务器上获取远程计算机的 IP
【发布时间】:2014-09-08 15:40:34
【问题描述】:

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

这是我用于初始化过程的代码(在同一域内工作)。非常感谢任何意见!

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 ...
    });
}

【问题讨论】:

  • 只需为这些远程计算机附加完整的域,例如machine.domain.tld,dns 会自动去查询domain.tld dns 服务器。如果你只是做machine,那么它将查询machine.jamie_still_domain.tld
  • 我已经试了一下。当我尝试在 ping 之前执行 Dns.GetHostEntry(Name + "." + CurrentConfig.DomainName) 时,它仍然返回 Host Not Found SocketExcpetions。问题的另一部分是它位于封闭的企业网络上,无法为托管我的应用程序所在机器的 DNS 区域提出查找区域(尝试在可以访问的单独托管域上使用实验室虚拟机进行测试) IP,但不是 DNS 名称)。
  • 我看了一下该线程,但想确保没有内置的 .NET 可以这样做,而不必为外部 DNS 请求构建新的命名空间或导入另一个库。感谢您的响应!

标签: c# dns remote-access


【解决方案1】:

跟进,我找到的解决方案来自Heijden's DNS Resolver。我编写了一个简单的 DnsManager 类,其中包含一个静态 GetIPAddress 方法,用于从 A 记录中提取 IP。

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 方法现在写成这样:

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

【讨论】:

    【解决方案2】:

    一旦您计划将专用服务器作为 DNS 源进行查询,您就必须远离默认库。在调查专用 DNS 请求后,我尝试了这个(如果没记错的话): http://www.codeproject.com/Articles/12072/C-NET-DNS-query-component

    【讨论】:

    • 现在查看这篇文章。感谢您的回复!
    • 这篇文章好像有点过时了。但是,@MarcB 引用的线程中link 的文章有一个完整且现代的解决方案。当我将解决方案集成到我的项目中时,我会更新我的帖子。谢谢大家!
    猜你喜欢
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 2013-05-18
    相关资源
    最近更新 更多