从 LAN 中的 IP 地址查找 DNS 主机名

发布于 2024-08-16 18:47:44 字数 1895 浏览 1 评论 0原文

嘿大家。我编写了一个程序,可以顺序扫描 LAN 的某些部分以查找计算机(将提供代码)。但是,当我运行此代码时,它仅返回运行它的计算机的 DNS 主机名。我研究过使用 WMI,但我不能,因为我并不总是拥有所找到的计算机的权限。有没有其他方法可以找到本地计算机的主机名?

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

非常感谢所有帮助。

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

All help is much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫性小仙女 2024-08-23 18:47:44

嗯 - 您的代码示例在我的机器上的行为符合预期 - 即它返回正在扫描的机器的主机名。

为了更深入地调查您的问题,您是否尝试过使用 nslookup 检查 IP 地址解析?

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

如果这不返回计算机名称,则您可能遇到与代码无关的名称解析问题。

如果这一切看起来没问题,那么可能还值得枚举 IpEntry.Aliases 集合。这里有任何条目吗?它们有意义吗?

最后 - 您上面的代码是否正是您出错的代码,或者它是一个“精炼”的示例?我问的原因是 Dns.GetHostEntry 的文档指出

“当传递空字符串作为
主机名,此方法返回
本地主机的 IPv4 地址。”

我还注意到您在静态中保存了“fullip”。如果这不是导致问题的确切代码,特别是如果它运行多线程,是否存在您有可能在调用 Dns.GetHostEntry 之前没有初始化“fullip”吗?

我可能离题很远,但我认为值得对我在查看您的代码时发生的事情进行脑力转储。问题:)

[编辑:] - 你对 kdt 的评论澄清了我的误解,我认为你是说你总是能取回本地计算机的主机名,无论如何。你“扫描”了哪台机器——事实上,我认为你是说你只是获取其他机器的IP地址(它们的IP地址),而只获取你本地的主机名。 这更容易解释

- 你的机器几乎肯定无法解析机器名称 - 我希望我建议的 nslookup 测试也不会返回机器名称。

为了将这些 IP 解析为主机名,您的计算机需要一个包含这些计算机条目的 DNS,或者将它们包含在其本地主机文件中;当您执行此调用时,您的计算机实际上并没有询问远程计算机的名称,因此如果没有常用名称解析路径之一的帮助,它将无法找到它。

它对我有用,因为我的本地 DNS 确实包含网络上所有计算机的条目,将它们的主机名解析为 IP 地址,反之亦然。

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

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

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

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?

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

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

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 :)

[EDIT:] - 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.

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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文