从局域网的IP地址查找DNS主机名 [英] Find DNS HostName from IP Address in LAN

查看:316
本文介绍了从局域网的IP地址查找DNS主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有。我写了一个程序,依次扫描局域网的某些部分为计算机(code将提供)。然而,当我运行此code,它只返回其上运行的计算机的DNS主机名。我看着使用WMI,但我不能,因为我不会总是被发现priveleges到计算机。是否有任何其他的方法来找到当地的计算机主机名?

Hey all. I have written a program that sequentially scans certain parts of a LAN for computers (code will be provided). However, when I run this code, it only returns the DNS HostName of the computer it is running on. I looked into using WMI, but I cannot, as I will not always have priveleges to the computers being found. Is there any other way to find a local computers HostName?

using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;

namespace CheckLocalNetwork
{
    class PingCheck
    {
        public static string fullip;

        public void CheckSequentialIP()
        {
            IPHostEntry IpEntry = Dns.GetHostEntry(fullip);

            Ping pingSender = new Ping();

            PingOptions options = new PingOptions();
            options.DontFragment = true;

            string data = "a";
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            int timeout = 120;
            PingReply reply = pingSender.Send(fullip, timeout, buffer, options);

            if (reply.Status == IPStatus.Success)
            {
                Console.WriteLine("Address: {0}", reply.Address.ToString());
                Console.WriteLine("Host Name: {0}", IpEntry.HostName);
                Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
                Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
                Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
                Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
                Console.WriteLine("");
            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Press enter to search for ip adresses that begin with 192.168.1");
            Console.ReadLine();

            for (int endofip = 1; endofip < 101; endofip++)
            {
                fullip = "192.168.1." + Convert.ToString(endofip);
                PingCheck checkfullip = new PingCheck();
                checkfullip.CheckSequentialIP();

            }
            Console.ReadLine();
    }

所有帮助是非常AP preciated。

All help is much appreciated.

推荐答案

嗯 - 你的code样品的行为与预期我的机器上 - 也就是说,它返回的机器的主机被扫描

Hmm - your code sample behaves as expected on my machine - i.e. it returns the hostname of the machine being scanned.

要更深层次的调查你的问题,你有没有尝试使用nslookup来检查IP地址解析?

To investigate your problem deeper, have you tried using nslookup to check the ip addresses resolve?

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Rob>nslookup                  <-- type this at a command prompt
Default Server:  mydns.mydomain.co.uk  <--- these two lines indicate the dns server being used to resolve your queries
Address:  192.168.0.1                  <----|

> 192.168.0.5                          <---- type in the ip address of one of the machines in question
Server:  mydns.mydomain.co.uk
Address:  192.168.0.1

Name:    myworkstation.mydomain.co.uk  <---- this is the hostname, as reported by the DNS using a reverse lookup
Address:  192.168.0.5

如果这不返回的机器名,那么你可能有一个不相关的code名称解析问题。

If this doesn't return the machine name, then you may have a name resolution issue that is not related to your code.

如果这一切看起来不错,那么它也可能是值得历数 IpEntry.Alias​​es 集合。是否有任何条目在这里,他们是否有意义?

If this all looks ok, then it might also be worth enumerating the IpEntry.Aliases collection. Are there any entries here, and do they make sense?

最后 - 是code你有上面完全相同的code,它是怎么了?你,还是一个蒸的例子吗?我想问的原因是, Dns.GetHostEntry 状态的文档

Finally - is the code you have above exactly the code that is going wrong for you, or is it a "distilled" example? The reason I ask is that the documentation for Dns.GetHostEntry states

当一个空字符串被当作   主机名,该方法返回   本地主机的IPv4地址。

"When an empty string is passed as the host name, this method returns the IPv4 addresses of the local host."

我也注意到你正握着fullip中有静。如果这不是确切的code,是造成这个问题,特别是如果这种运行多线程的,有你没有在 DNS初始化fullip的机会。 GetHostEntry 被称为?

I also notice you're holding "fullip" in a static. If this is not the exact code that is causing the problem, especially if this runs multithreaded, is there a chance you are not initialising "fullip" before the Dns.GetHostEntry is called?

我可能是遥远,但我的想法是值得给予什么发生在我一个倾吐心事,因为我看了你的问题:)

I may be way off, but I thought is was worth giving a brain dump of what occured to me as I looked at your problem :)

[编辑:] - 您的评论,以KDT澄清了一些东西,我误解。我的认为的你说你总是回来的主机名本地机器上,无论哪个机器你扫描 - 这是非常奇怪的行为。事实上,我认为你的意思是你刚刚回来的其他机器的IP地址(其IP地址),并且只得到一个主机名当地。无视我对线程和空参数最后一位。

[] - your comment to kdt has clarified something I misunderstood. I thought you were saying you always got back the hostname for your local machine, no matter which machine you "scanned" - which is very odd behaviour. In fact I think you are saying you just get back IP addresses for other machines (their IP address), and only get a hostname for your local. Disregard my last bit about the threading and the empty argument.

这是更容易解释 - 你的机器几乎可以肯定只是没有能够解决机器的名称 - 我希望我的NSLOOKUP测试,我建议将不会返回任何计算机名。

This is far more easily explained - your machine is almost certainly just not able to resolve the machine names - I expect my nslookup test I suggested will not return the machine names either.

为了解决这些IP的为主机名,你的机器需要一个DNS有条目这些机器,或者让他们在本地hosts文件;你的机器是不是真正的要求的远程计算机的名称,当你做这个调用,因此深得; T为能够找到它,而不会从它的常用名称解析路径中的一个帮助。

In order to resolve these IP's to host names, your machine needs a DNS that has entries for these machines, or to have them in its local hosts file; your machine isn't actually asking the remote machine for its name when you do this call so it won;t be able to find it out without help from one of its usual name resolution paths.

这对我的作品,因为我的本地DNS确实有项我的网络上的所有计算机,解决他们的主机名,IP地址,反之亦然。

It works for me, because my local DNS really does have entries for all the machines on my network, resolving their host names to ip addresses and vice-versa.

这篇关于从局域网的IP地址查找DNS主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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