Home > C#

Resolve IP Address And Host Name From MAC Address using C# and Windows ARP Utility

8. November 2009

While working on the Virtual Router project, I’ve come across a need to be able to retrieve the IP Address and Host Name of a given machine on the local network when only the machines MAC Address is known. This took a bit of research to figure out, and eventually I stumbled across the “arp.exe” utility within Windows.

“arp.exe” uses the Address Resolution Protocol to provide functionality to add, delete or display the IP address for MAC (Media Access Control) address translation.

To see the IP / MAC Address translations, just open up the Command Prompt in Windows, and type “arp –a” and press enter.

Now, you may be asking, how do we use this utility from within a .NET application? Well, it’s rather simple. All you need to do is use the System.Diagnostics.Process class to execute the “arp –a” call and retrieve the results. Then just parse through the results and get out the IP Address for the desired MAC Address.

I’m not going to give full detail on how to call “arp” from .NET. Below is an example on using the “IPInfo” class I created to perform the previously mentioned method of retrieving the IP Address for a specified MAC Address. I also added in the ability to get the machines Host Name using the the “Dns.GetHostEntry” method.

var ipinfo = IPInfo.GetIPInfo(txtMAC.Text);

if (ipinfo == null)
{
    // Machine Not Found!
}
else
{
    var ip = ipinfo.IPAddress;
    var hostname = ipinfo.HostName;
}


Below is the full code of my “IPInfo” class:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;

/// <summary>
/// This class allows you to retrieve the IP Address and Host Name for a specific machine on the local network when you only know it's MAC Address.
/// </summary>
public class IPInfo
{
    public IPInfo(string macAddress, string ipAddress)
    {
        this.MacAddress = macAddress;
        this.IPAddress = ipAddress;
    }

    public string MacAddress { get; private set; }
    public string IPAddress { get; private set; }

    private string _HostName = string.Empty;
    public string HostName
    {
        get
        {
            if (string.IsNullOrEmpty(this._HostName))
            {
                try
                {
                    // Retrieve the "Host Name" for this IP Address. This is the "Name" of the machine.
                    this._HostName = Dns.GetHostEntry(this.IPAddress).HostName;
                }
                catch
                {
                    this._HostName = string.Empty;
                }
            }
            return this._HostName;
        }
    }


    #region "Static Methods"

    /// <summary>
    /// Retrieves the IPInfo for the machine on the local network with the specified MAC Address.
    /// </summary>
    /// <param name="macAddress">The MAC Address of the IPInfo to retrieve.</param>
    /// <returns></returns>
    public static IPInfo GetIPInfo(string macAddress)
    {
        var ipinfo = (from ip in IPInfo.GetIPInfo()
                  where ip.MacAddress.ToLowerInvariant() == macAddress.ToLowerInvariant()
                  select ip).FirstOrDefault();

        return ipinfo;
    }

    /// <summary>
    /// Retrieves the IPInfo for All machines on the local network.
    /// </summary>
    /// <returns></returns>
    public static List<IPInfo> GetIPInfo()
    {
        try
        {
            var list = new List<IPInfo>();

            foreach (var arp in GetARPResult().Split(new char[] { '\n', '\r' }))
            {
                // Parse out all the MAC / IP Address combinations
                if (!string.IsNullOrEmpty(arp))
                {
                    var pieces = (from piece in arp.Split(new char[] { ' ', '\t' })
                                  where !string.IsNullOrEmpty(piece)
                                  select piece).ToArray();
                    if (pieces.Length == 3)
                    {
                        list.Add(new IPInfo(pieces[1], pieces[0]));
                    }
                }
            }

            // Return list of IPInfo objects containing MAC / IP Address combinations
            return list;
        }
        catch(Exception ex)
        {
            throw new Exception("IPInfo: Error Parsing 'arp -a' results", ex);
        }
    }

    /// <summary>
    /// This runs the "arp" utility in Windows to retrieve all the MAC / IP Address entries.
    /// </summary>
    /// <returns></returns>
    private static string GetARPResult()
    {
        Process p = null;
        string output = string.Empty;

        try
        {
            p = Process.Start(new ProcessStartInfo("arp", "-a")
            {
                CreateNoWindow = true,
                UseShellExecute = false,
                RedirectStandardOutput = true
            });

            output = p.StandardOutput.ReadToEnd();

            p.Close();
        }
        catch(Exception ex)
        {
            throw new Exception("IPInfo: Error Retrieving 'arp -a' Results", ex);
        }
        finally
        {
            if (p != null)
            {
                p.Close();
            }
        }
        
        return output;
    }

    #endregion
}

C# , ,



Comments

12/6/2009 2:51:59 PM #
Pretty Interesting post. Couldnt be written any better. Thanks for sharing!
12/6/2009 11:39:19 PM #
I admire what you have done here.



Regards
Promi



12/7/2009 3:13:54 AM #
I want to express my admiration of your writing skill and ability to make reader to read the while thing to the end


Regards
Martin

12/7/2009 4:20:25 AM #
I would like to read more of your blogs and to share my thoughts with you


Regards
Yost

12/11/2009 9:13:00 PM #
WOW your page came up first in Google. And this is what I was looking for.
12/12/2009 1:31:13 AM #
I always wanted to write in my site something like that but I guess you'r faster Smile
12/12/2009 5:13:39 AM #
Great fix.
12/12/2009 9:13:28 PM #
Good Day, It looks like this page does not show nicely in ie8.
12/12/2009 10:08:04 PM #
Looks like an interesting blog. Will make visit again.
12/12/2009 10:59:36 PM #
Hi There, It seem blog doesnt display nicely in ie6.
12/13/2009 2:08:32 AM #
Good Day, It seem this url does not display decently inside internet explorer6 .
12/13/2009 1:28:02 PM #
Loved reading this post.
12/17/2009 6:37:08 PM #
Although I do agree with your post, I have my own reservations.
12/17/2009 11:24:25 PM #
I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post
12/18/2009 10:11:34 PM #
Just wanted to give you a shout from the valley of the sun, great information. Much appreciated.
12/19/2009 8:16:58 PM #
Great insights. I loved to read your article. You must be putting a lot of time into your blog!
12/20/2009 2:18:11 PM #
Great share. Keep up the good work.
12/20/2009 11:12:59 PM #
Your post give me some idea. Thank you.
12/20/2009 11:38:13 PM #
Excellent post.
12/21/2009 12:38:55 AM #
Great post. I like it.
12/21/2009 12:04:23 PM #
The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!
12/22/2009 4:18:16 AM #
Wow, Thanks a lot for the coding to resolve IP Address and host name from MAC address. I am newbie to C#, This helps.
12/22/2009 5:53:44 AM #
Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.
12/28/2009 2:33:18 PM #
Merry Christmas admin and a Happy New Year. I will be coming back to read more of your interesting post
12/29/2009 7:51:11 PM #
I love reading your posts. I wish you a Happy New Year!
12/29/2009 9:19:05 PM #
Do you make money out of this blog? just curious
12/29/2009 11:06:56 PM #
amazing blogs you got
12/30/2009 3:44:40 AM #
I like your blog so much that I feel I have to wish you. Happy New Year in advance. Have a nice and prosperous year ahead
1/2/2010 9:22:07 AM #
Hi,

How I can find female travelling companion to travel in India.?
1/6/2010 9:22:48 AM #
You write very interestingly. I think Google is becoming very smart. It can sense which website has interesting posts Smile
1/7/2010 4:00:04 AM #
How to find the ip address of the sender who has sent fake email messages?
1/7/2010 4:51:17 AM #
How to start a dedicated counter strike server and how to identify the ip adress?
1/9/2010 9:30:02 AM #
How is the new year going? I hope to read more interesting posts like last year
1/9/2010 10:16:23 PM #
I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.
1/12/2010 6:46:38 AM #
You write very well. Kept me really engaged for some time Smile
1/17/2010 5:35:35 PM #
Its really useful Post. Thank you for the nice Post…
1/17/2010 5:36:07 PM #
Its really useful Post. Thank you for the nice Post…
1/17/2010 5:59:17 PM #
Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful.
1/19/2010 9:52:59 PM #
Starting to understand a bit more now... Thanks for keeping it simple!
1/22/2010 2:30:57 PM #
Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts.
1/22/2010 10:16:36 PM #
Amazing posts you got in your blog. Will visit again.
1/23/2010 11:12:07 PM #
The post is written in very a good manner and it entails many useful information for me. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. Thank you for the post.
1/23/2010 11:14:37 PM #
Post very nicely written, and it contains useful facts. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement. Thanks for sharing with us.
1/23/2010 11:34:25 PM #
I was actually looking around for a blog post on this issue and luckily stumbled across your post! I’m actually quite interested so will keep an eye out for updates.
1/23/2010 11:34:38 PM #
Wow nice info you have here. I hope this will help a lot of people. I will tell my friends to read this. Thanks!
1/27/2010 12:15:54 AM #
I am not much of a guy who thinks in so deeply about web design but I think your post had some valid points in it. Like designers are forced to design stuff within the limited code available and not go beyond it, their innovation is somewhat limited but still I think Web Design won't die! I agree that Amazon and other some big sites won't have a blog but now a days it's very important to have some sort of option available so people can quickly communicate their thoughts. I think Amazon if wants to shift it to that, they can get a customized CMS for themselves.
1/27/2010 10:40:09 PM #
Well, one of the best article l have come across on this worthwhile topic. I quite agree with your assumptions and will thirstily look forward to your coming updates. Just saying thanks will not just be adequate, for the tremendous clarity in your writing. I will directly grab your rss feed to stay privy of any updates. Fabulous work and much success in your business efforts!
1/28/2010 10:23:14 AM #
This a little bit funny. I found your site via search engine a few moment ago, and luckily, this is the only information I was looking for the last hours.
1/28/2010 3:32:34 PM #
Failing to plan is planning to fail.
1/30/2010 6:21:41 AM #
I am not much of a guy who thinks in so deeply about web design but I think your post had some valid points in it. Like designers are forced to design stuff within the limited code available and not go beyond it, their innovation is somewhat limited but still I think Web Design won't die! I agree that Amazon and other some big sites won't have a blog but now a days it's very important to have some sort of option available so people can quickly communicate their thoughts. I think Amazon if wants to shift it to that, they can get a customized CMS for themselves.
1/30/2010 11:40:30 PM #
I must say that this is a great post. I loved reading it. You have done a great job.
1/31/2010 5:34:19 PM #
Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful.
1/31/2010 9:13:18 PM #
nice blog, very helpfull information, keep share, thanks
2/1/2010 8:37:13 PM #
This is a cool screen idea ! It is very interesting indeed.Thank you for your info.i love to read all info.This article gives the light in which we can observe the reality.
2/1/2010 10:04:06 PM #
Nice to be visiting your blog again, it has been months for me. Well this article that i've been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share.
2/2/2010 11:18:49 AM #
hi,
Excellent blog post, I look forward to reading more.
2/2/2010 12:47:12 PM #
Wow nice info you have here. I hope this will help a lot of people. I will tell my friends to read this. Thanks
2/2/2010 7:02:33 PM #
Insightful piece, thanks a lot!
2/3/2010 12:14:46 AM #
That is some inspirational stuff. Never knew that opinions could be this varied. Thanks for all the enthusiasm to offer such helpful information here.
2/3/2010 1:09:50 PM #
Insightful piece, thanks a lot!
SEO
2/5/2010 6:31:03 AM #
Really appreciate this post. It’s hard to sort the good from the bad sometimes, but I think you’ve nailed it!

2/5/2010 7:17:58 AM #
Appreciate the info, it’s good to know.
2/5/2010 8:20:43 AM #
Always worth visiting your blog to read your cool posts. Keep them coming.
2/6/2010 2:20:14 AM #
I appreciate your efforts!!! You took so much effort and wrote this post to make all us know about this.  
This is one of the greatest post that I've ever read!!!!
2/6/2010 3:37:42 AM #
Nice to be visiting your blog again, it has been months for me. Well this article that i've been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share.
2/6/2010 11:34:14 AM #
You have got some great posts in your blog. I will be visiting again.
Comments are closed