从 LAN 中的 IP 地址查找 DNS 主机名
嘿大家。我编写了一个程序,可以顺序扫描 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯 - 您的代码示例在我的机器上的行为符合预期 - 即它返回正在扫描的机器的主机名。
为了更深入地调查您的问题,您是否尝试过使用 nslookup 检查 IP 地址解析?
如果这不返回计算机名称,则您可能遇到与代码无关的名称解析问题。
如果这一切看起来没问题,那么可能还值得枚举
IpEntry.Aliases
集合。这里有任何条目吗?它们有意义吗?最后 - 您上面的代码是否正是您出错的代码,或者它是一个“精炼”的示例?我问的原因是
Dns.GetHostEntry
的文档指出我还注意到您在静态中保存了“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?
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
statesI 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.