C# 从LAN中的IP地址查找DNS主机名
大家好。我已经写了一个程序,顺序扫描计算机局域网的某些部分(代码将提供)。但是,当我运行这段代码时,它只返回运行它的计算机的DNS主机名。我研究过使用WMI,但我不能,因为我不会一直拥有被发现的计算机的特权。有没有其他方法可以找到本地计算机的主机名C# 从LAN中的IP地址查找DNS主机名,c#,.net,network-programming,dns,lan,C#,.net,Network Programming,Dns,Lan,大家好。我已经写了一个程序,顺序扫描计算机局域网的某些部分(代码将提供)。但是,当我运行这段代码时,它只返回运行它的计算机的DNS主机名。我研究过使用WMI,但我不能,因为我不会一直拥有被发现的计算机的特权。有没有其他方法可以找到本地计算机的主机名 using System; using System.Net; using System.Net.NetworkInformation; using System.Text; namespace CheckLocalNetwork { cl
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();
}
使用系统;
Net系统;
使用System.Net.NetworkInformation;
使用系统文本;
名称空间检查本地网络
{
类PingCheck
{
公共静态字符串fullip;
公共无效检查顺序IP()
{
IPHostEntry IpEntry=Dns.GetHostEntry(fullip);
Ping pingSender=新Ping();
PingOptions=新的PingOptions();
options.DontFragment=true;
字符串数据=“a”;
byte[]buffer=Encoding.ASCII.GetBytes(数据);
int超时=120;
PingReply reply=pingSender.Send(完整IP、超时、缓冲区、选项);
if(reply.Status==IPStatus.Success)
{
WriteLine(“地址:{0}”,reply.Address.ToString());
WriteLine(“主机名:{0}”,IpEntry.HostName);
WriteLine(“往返时间:{0}”,reply.RoundtripTime);
WriteLine(“生存时间:{0}”,reply.Options.Ttl);
WriteLine(“不分段:{0}”,reply.Options.DontFragment);
WriteLine(“缓冲区大小:{0}”,reply.Buffer.Length);
控制台。写线(“”);
}
}
静态void Main(字符串[]参数)
{
WriteLine(“按enter键搜索以192.168.1开头的ip地址”);
Console.ReadLine();
对于(int endofip=1;endofip<101;endofip++)
{
fullip=“192.168.1.”+转换为字符串(endofip);
PingCheck checkfullip=新的PingCheck();
checkfullip.CheckSequentialIP();
}
Console.ReadLine();
}
非常感谢您的帮助。嗯-您的代码示例在我的机器上的行为与预期的一样-即,它返回被扫描机器的主机名 为了更深入地调查您的问题,您是否尝试使用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
Microsoft Windows[版本6.1.7600]
版权所有(c)2009微软公司。保留所有权利。
C:\Users\Rob>nslookup我在写另一个“不是这样”当您的编辑出现时,请发表评论。您是对的,nslookup没有返回任何对运行我的程序时出现的其他地址有用的内容。这是整个程序。建议一个正确的问题来解决此问题,然后?这里需要一种不使用WMI获取DNS主机名的可靠方法,这听起来仍然很有用比如。这里的问题实际上与代码无关——这与您的网络设置有关。基本上,如果网络上的一台机器想要另一台机器的主机名,只给出其ip地址,它只有几个选择。1)查看本地主机文件中是否有映射,这对您的情况没有帮助。2)询问DNS。除了一些传统的NetBIOS方法之外s、 这就是你所拥有的一切。WMI也不会有任何帮助。这确实是一个系统管理问题-你的DNS服务器应该有这些机器的条目。我真的想不出一个解决方案,如果没有这一点的话。非常感谢你的所有帮助。实际上-对我上面的评论进行微小的更改-如果你可以像使用WMI来解决这个问题一样,WMI将有所帮助实际上,连接到远程计算机并询问它的主机名(我很确定你无论如何都可以)。但是在你提到的WMI特权问题和DNS问题之间,我担心这两种解决方案都不在你的掌握之中。