C# Ping或其他方式通过C中的MAC判断设备是否在网络上#
我正在开发一个家庭安全应用程序。我想做的一件事是根据我是否在家自动关闭和打开它。我有一部带有Wifi的手机,当我在家时,它会自动连接到我的网络 手机通过DHCP连接并获取其地址。虽然我可以将其配置为使用静态IP,但我不希望这样。在C#/.Net中是否有任何类型的“Ping”或等效程序可以获取设备的MAC地址,并告诉我设备当前是否在网络上处于活动状态 编辑:为了澄清,我在电脑上运行软件,希望能够在同一局域网上检测手机 编辑:这是我想出的代码,多亏了佩尔森的帮助。它能可靠地检测出我感兴趣的手机是否在家里C# Ping或其他方式通过C中的MAC判断设备是否在网络上#,c#,.net,ping,mac-address,C#,.net,Ping,Mac Address,我正在开发一个家庭安全应用程序。我想做的一件事是根据我是否在家自动关闭和打开它。我有一部带有Wifi的手机,当我在家时,它会自动连接到我的网络 手机通过DHCP连接并获取其地址。虽然我可以将其配置为使用静态IP,但我不希望这样。在C#/.Net中是否有任何类型的“Ping”或等效程序可以获取设备的MAC地址,并告诉我设备当前是否在网络上处于活动状态 编辑:为了澄清,我在电脑上运行软件,希望能够在同一局域网上检测手机 编辑:这是我想出的代码,多亏了佩尔森的帮助。它能可靠地检测出我感兴趣的手机是否在
private bool PhonesInHouse()
{
Ping p = new Ping();
// My home network is 10.0.23.x, and the DHCP
// pool runs from 10.0.23.2 through 10.0.23.127.
int baseaddr = 10;
baseaddr <<= 8;
baseaddr += 0;
baseaddr <<= 8;
baseaddr += 23;
baseaddr <<= 8;
// baseaddr is now the equivalent of 10.0.23.0 as an int.
for(int i = 2; i<128; i++) {
// ping every address in the DHCP pool, in a separate thread so
// that we can do them all at once
IPAddress ip = new IPAddress(IPAddress.HostToNetworkOrder(baseaddr + i));
Thread t = new Thread(() =>
{ try { Ping p = new Ping(); p.Send(ip, 1000); } catch { } });
t.Start();
}
// Give all of the ping threads time to exit
Thread.Sleep(1000);
// We're going to parse the output of arp.exe to find out
// if any of the MACs we're interested in are online
ProcessStartInfo psi = new ProcessStartInfo();
psi.Arguments = "-a";
psi.FileName = "arp.exe";
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
bool foundone = false;
using (Process pro = Process.Start(psi))
{
using (StreamReader sr = pro.StandardOutput)
{
string s = sr.ReadLine();
while (s != null)
{
if (s.Contains("Interface") ||
s.Trim() == "" ||
s.Contains("Address"))
{
s = sr.ReadLine();
continue;
}
string[] parts = s.Split(new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
// config.Phones is an array of strings, each with a MAC
// address in it.
// It's up to you to format them in the same format as
// arp.exe
foreach (string mac in config.Phones)
{
if (mac.ToLower().Trim() == parts[1].Trim().ToLower())
{
try
{
Ping ping = new Ping();
PingReply pingrep = ping.Send(parts[0].Trim());
if (pingrep.Status == IPStatus.Success)
{
foundone = true;
}
}
catch { }
break;
}
}
s = sr.ReadLine();
}
}
}
return foundone;
}
private bool PhonesInHouse()
{
Ping p=新Ping();
//我的家庭网络是10.0.23.x,DHCP
//池从10.0.23.2运行到10.0.23.127。
int baseaddr=10;
BaseAdDr.P>您可以使用它来广播谁拥有那个MAC地址的请求。所有者将用它的IP地址进行响应。我不知道推荐一个RARP工具或库,所以您可以考虑编写自己的网络层来发送/接收这些包。 < P>一个不同的方法是使用<代码> ping <代码>和<代码> ARP工具。由于ARP数据包只能停留在同一广播域中,因此您可以ping网络的广播地址,每个客户端都将使用ARP响应进行回复。这些响应中的每一个都缓存在您的ARP表中,您可以使用命令ARP-a
查看这些响应。因此:
rem Clear ARP cache
netsh interface ip delete arpcache
rem Ping broadcast addr for network 192.168.1.0
ping -n 1 192.168.1.255
rem View ARP cache to see if the MAC addr is listed
arp -a
其中一些可以在托管代码中完成,例如在命名空间中
注意:通过清除其他本地服务器的缓存条目,清除ARP缓存可能会对网络性能产生轻微影响。但是,通常每隔20分钟或更短时间清除一次缓存。这个问题可能会对您有所帮助。它将帮助您从IP获取MAC。或者使用“ARP-a”然后解析输出。你能用另一种方法吗?比如,在你的手机上有一个应用程序,可以检测它何时在家庭网络上,然后告诉你家里的应用程序它在范围内?通常你可以让手机在连接时通知你。这是脚本的最终版本吗?不是真的;我把它分成两部分-ping线程将循环通过DHCP空间,每秒ping每个地址一次,然后是解析ARP.exe输出的主要部分。我这样做是因为这里给出的ping每个地址的技术遇到了限制和其他限制问题。您需要原始套接字来完成此操作-请在完成此操作后通知我们因为我们想看看,好了,现在我在家,有时间玩这个,我刚刚试过了,它只使用我的台式电脑和文件服务器。它错过了网络上的大多数其他设备(无线适配器、电话、打印机、其他笔记本电脑、两台虚拟机…)它接收到的一个设备有两个交换机。要进行测试,请尝试通过IP ping一个丢失的设备,然后检查arp-a
。如果这样做有效,则设备可能不会响应广播ping。/也许这毕竟不是最好的方法。