1

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?

Kulahan
  • 512
  • 1
  • 9
  • 23

4 Answers4

1

You need to use RARP http://en.wikipedia.org/wiki/Reverse_Address_Resolution_Protocol

and

MAC address to IP Address on Same LAN in C#

Community
  • 1
  • 1
pm100
  • 48,078
  • 23
  • 82
  • 145
1

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;
Tim Coker
  • 6,484
  • 2
  • 31
  • 62
  • Well the servers that I'm going to be pinging do have static names. How do you ping based on a saved list of names? – Kulahan Aug 30 '11 at 20:52
  • I added a function that will ping the first available address returned from DNS resolution. HTH! – Tim Coker Aug 31 '11 at 14:10
  • Wow, this is great! Out of curiosity, though, what libraries are you using to get this to work? VS isn't recognizing IPAddress, DNS.GetHostAddress(Name), or (SocketException). – Kulahan Aug 31 '11 at 22:22
  • Added list of namespaces I'm referencing. – Tim Coker Sep 01 '11 at 11:58
  • Not a problem. Weird how you come asking a specific question sometimes (reverse arp resolution) and wind up with a different solution than what you planned! :) Thanks for the accept. Can you give me a vote up? (will put me over 2600 rep) – Tim Coker Sep 01 '11 at 14:45
  • Interesting fact, my rep is not high enough yet to vote yours up, or else I'd love to. (I just signed up for the site a few days ago) – Kulahan Sep 01 '11 at 21:25
0

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

yoozer8
  • 7,361
  • 7
  • 58
  • 93
  • I was hoping to avoid having to use the MAC addres to find the IP address. It seems like unnecessary overhead. Is this really the -only- solution? – Kulahan Aug 30 '11 at 20:00
0

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

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122