根据 MA​​C 地址 Ping 网络上的节点

发布于 2024-12-02 16:30:17 字数 385 浏览 1 评论 0原文

我希望用户能够输入网络上计算机的 MAC。一旦他们完成了这一操作,它就会将其添加到列表中。然后,每次调用该类时,程序都会对列表中的所有 MAC 进行 ping 操作(我知道这不一定是可能的,但请继续阅读)。

通常我只使用 IP 地址,但它们不是静态的,并且网络上有很多设备,我不关心该程序的连接性。如果他们没有回复,我希望它弹出一个消息框。

现在,话虽这么说,我唯一遇到麻烦的部分是我 ping 某些内容的实际部分。我知道对于 MAC 地址而言,实际的“ping”是不可能的,那么我如何检查类似的情况呢?或者,如果更容易的话,我也可以接受根据计算机名称执行 ping 操作。

编辑:我不想使用像 arp 这样的东西来查找我想要的设备的 IP 地址。正如我所说,我也对是否可以按名称搜索设备感兴趣。那行得通吗?

I'd like the user to be able to enter the MAC of a computer on the network. Once they've done that, it'll add it to a list. The program will then ping all of those MACs on the list every time the class is called (I know this isn't necessarily possible, but read on).

Normally I'd simply use IP addresses, but they aren't static, and there are a -lot- of devices on the network that I don't care about the connectivity of for this program. If they don't respond, I'd like it to pop up a message box.

NOW, that being said, the only part I'm having trouble with is the actual part where I ping something. I know that an actual "ping" is not possible when it comes to MAC addresses, so how could I check for something like that? Alternatively, if it's easier, I could also accept pinging something based on the computer name.

EDIT: I'd prefer not to have to use things like arp to find the IP addresses of the devices I want. Like I said though, I'm also interested in whether or not it's possible to search for devices by name. Would that work?

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

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

发布评论

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

评论(4

握住我的手 2024-12-09 16:30:17

如果您有适当管理的环境,则应该使用名称。我所说的正确管理的环境主要是指在您的本地网络上拥有一个 DNS 服务器。

我有类似的运行来 ping 工业以太网设备。这些是静态分配的地址,因此它们不会像 DHCP 客户端那样向 DNS 注册。我让 DNS 管理员为他们创建记录,这样我就可以使用他们的名字。从长远来看,你会过得更好,因为两年后你将不知道列表中的 mac 地址指的是什么。创建名称时,您可以根据需要使它们具有描述性。

编辑:这是一个函数,它将名称作为字符串,从 DNS 查找关联的 IP,然后进行 ping 操作。如果 DNS 解析失败或 ping 未报告成功,则该函数返回 false。否则返回 true。顺便说一句,您还应该记录异常情况以便稍后进行故障排除。

public bool Check(string Name)
{
    //try dns resolution, if fails, quit reporting error
    IPAddress[] addresses = null;
    try
    {
        addresses = Dns.GetHostAddresses(Name);
    }
    catch (SocketException)
    {
        return false;
    }

    //ping remote address
    PingReply reply = ping.Send(addresses[0]);
    switch (reply.Status)
    {
        case IPStatus.Success:
            return true;
            break;
        default:
            return false;
            break;
    }
}

编辑2:这是我在这个项目中使用的命名空间。不确定具体在哪里,但添加这三个将使一切顺利。

using System.Net.NetworkInformation;
using System.Net;
using System.Net.Sockets;

If you have a properly administered environment, you should be using names. By properly administered environment, I'm primarily meaning having a DNS server on your local network.

I have something similar running that pings industrial ethernet devices. These are statically assigned addresses, so they don't register themselves with DNS as a DHCP client would. I had our DNS administrator create records for them so I can just use their name. You'll be better off in the long run as two years from now you're going to have NO idea what that mac address in your list was referring to. When creating names, you can make them as descriptive as necessary.

EDIT: Here's a function that takes a name as a string, looks up the associated IP from DNS, then pings. If DNS resolution fails or the ping doesn't report success, the function returns false. It returns true otherwise. You should also log the exceptions for troubleshooting later, BTW.

public bool Check(string Name)
{
    //try dns resolution, if fails, quit reporting error
    IPAddress[] addresses = null;
    try
    {
        addresses = Dns.GetHostAddresses(Name);
    }
    catch (SocketException)
    {
        return false;
    }

    //ping remote address
    PingReply reply = ping.Send(addresses[0]);
    switch (reply.Status)
    {
        case IPStatus.Success:
            return true;
            break;
        default:
            return false;
            break;
    }
}

EDIT 2: Here are the namespaces I'm using in this project. Not sure what's where exactly, but adding these three will get everything going.

using System.Net.NetworkInformation;
using System.Net;
using System.Net.Sockets;
も星光 2024-12-09 16:30:17

您需要使用 RARP http://en.wikipedia.org/wiki/Reverse_Address_Resolution_Protocol

<一个href="https://stackoverflow.com/questions/2204318/mac-address-to-ip-address-on-same-lan-in-c">C# 中同一 LAN 上的 MAC 地址到 IP 地址

百合的盛世恋 2024-12-09 16:30:17

根据网络的不同,您也许能够使用 arp 缓存来查找给定 MAC 地址的 IP(即使您的网络设置为您可以执行此操作,但只有在另一个 MAC 地址的情况下,它才会起作用)请求时机器位于缓存中)。您可以在 Wikipedia 上了解有关 arp 的信息

Depending on the network, you may be able to use the arp cache to look up the IP of a given MAC address (even if your network is set up such that you could do this, it will only work if the MAC address of the other machine is in the cache at the time of the request). You can read about arp on Wikipedia

只有一腔孤勇 2024-12-09 16:30:17

如果您有 MAC 地址,则可以使用 MAC 地址获取 IP 地址和计算机名称。获得 IP 后,您可以简单地 ping 机器。

有关如何从 MAC 地址获取 IP 地址的示例,请参阅代码

If you have the MAC address you can get the IP address and machine name using MAC address. Once you have the IP you can simply ping the machine.

See this code for an example of how to get IP address from MAC address

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