• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 995
  • Last Modified:

Get computer name from MAC address

Is it possible to get the computer name from a MAC address?
0
JohnnyLai
Asked:
JohnnyLai
1 Solution
 
jkrCommented:
You can obtain the IP address from a MAC address, and from there it is only a small step to get a name from that IP address. See http://www.codeproject.com/KB/IP/ARPTables.aspx ("Edit (Add/Remove/Modify) ARP Tables") for the MAC-to-IP part. Once you have the IP address, you can use 'gethostbyaddr()' (http://msdn.microsoft.com/en-us/library/ms738521.aspx). E.g. (like on the MSDN page):
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
 
int main(int argc, char **argv)
{
 
    //-----------------------------------------
    // Declare and initialize variables
    WSADATA wsaData;
    int iResult;
 
    DWORD dwError;
 
    struct hostent *remoteHost;
    char *host_name;
    struct in_addr addr;
 
    // Validate the parameters
    if (argc != 2) {
        printf("usage: %s ipv4 address\n", argv[0]);
        printf("  to return the host\n");
        printf("       %s 127.0.0.1\n", argv[0]);
        return 1;
    }
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }
 
    host_name = argv[1];
 
// If the user input is an alpha name for the host, use gethostbyname()
// If not, get host by addr (assume IPv4)
    if (isalpha(host_name[0])) {        /* host address is a name */
        printf("Calling gethostbyname with %s\n", host_name);
        remoteHost = gethostbyname(host_name);
    } else {
        printf("Calling gethostbyaddr with %s\n", host_name);
        addr.s_addr = inet_addr(host_name);
        if (addr.s_addr == INADDR_NONE) {
            printf("The IPv4 address entered must be a legal address\n");
            return 1;
        } else
            remoteHost = gethostbyaddr((char *) &addr, 4, AF_INET);
    }
 
    if (remoteHost == NULL) {
        dwError = WSAGetLastError();
        if (dwError != 0) {
            if (dwError == WSAHOST_NOT_FOUND) {
                printf("Host not found\n");
                return 1;
            } else if (dwError == WSANO_DATA) {
                printf("No data record found\n");
                return 1;
            } else {
                printf("Function failed with error: %ld\n", dwError);
                return 1;
            }
        }
    } else {
        printf("Function returned:\n");
        printf("\tOfficial name: %s\n", remoteHost->h_name);
        printf("\tAlternate names: %s\n", remoteHost->h_aliases);
        printf("\tAddress type: ");
        switch (remoteHost->h_addrtype) {
        case AF_INET:
            printf("AF_INET\n");
            break;
        case AF_INET6:
            printf("AF_INET\n");
            break;
        case AF_NETBIOS:
            printf("AF_NETBIOS\n");
            break;
        default:
            printf(" %d\n", remoteHost->h_addrtype);
            break;
        }
        printf("\tAddress length: %d\n", remoteHost->h_length);
        addr.s_addr = *(u_long *) remoteHost->h_addr_list[0];
        printf("\tFirst IP Address: %s\n", inet_ntoa(addr));
    }
 
    return 0;
}

Open in new window

0
 
JohnnyLaiAuthor Commented:
Thansk! I will try that.
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now