IP and MAC address retrieval for dummies

If under Microsoft Windows download C/C++ compiler. Read User docs and compile

Under UNIX, Linux & Mac OSx compile and run as follow …
Compiler command
bash-4.4# cc -o ipmaw ipmaw.c
Run command
bash-4.4# ipmaw
or
bash-4.4# ./ipmacaw

IP Address of eth0: 243.127.0.0 and MAC address: f0 4d a2 9b 99 70

IP Address of wlan0: 192.168.1.18 and MAC address: 1c 65 9d 89 73 f9

IP Address of lo: 127.0.0.1 and MAC address: 00 00 00 00 00 00

bash-4.4#

    /*
     * # Copyright (c) 2000-2011 Prof. Horstmann <wmh (at) ormae.org>
     * 
     * ipmacaw.c
     * C Program to Get IP & MAC Addresses
     * by Prof. HORSTMANN, for educational purpose
     * Code can be shortened by much, so do not contact me on that.
     * Students have for GOAL to reduce code, and explain why.
     */
    
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <unistd.h>
#include <arpa/inet.h>

    int main()

    {

        int n;
        int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);

        struct ifreq ifr;
        
        struct ifreq s;

        // IP address for eth0
        char array[] = "eth0";

        n = socket(AF_INET, SOCK_DGRAM, 0);

        //Type of address to retrieve - IPv4 IP address

        ifr.ifr_addr.sa_family = AF_INET;

        //Copy the interface name in the ifreq structure

        strncpy(ifr.ifr_name , array , IFNAMSIZ - 1);

        ioctl(n, SIOCGIFADDR, &ifr);

        close(n);

        //display result

        printf("\nIP Address of %s: %s and MAC address: " , array , inet_ntoa(( (struct sockaddr_in *)&ifr.ifr_addr )->sin_addr) );
        // End IP address for eth0

        // MAC address for 
        strcpy(s.ifr_name, "eth0");
        
        if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
        int i;
        for (i = 0; i < 6; ++i)
        printf(" %02x", (unsigned char) s.ifr_addr.sa_data[i]);
        puts("\n");
        }
        // End MAC address for 
    
        // IP address for wlan0
        char array1[] = "wlan0";

        n = socket(AF_INET, SOCK_DGRAM, 0);

        //Type of address to retrieve - IPv4 IP address

        ifr.ifr_addr.sa_family = AF_INET;

        //Copy the interface name in the ifreq structure

        strncpy(ifr.ifr_name , array1 , IFNAMSIZ - 1);

        ioctl(n, SIOCGIFADDR, &ifr);

        close(n);

        //display result

        printf("IP Address of %s: %s and MAC address: " , array1 , inet_ntoa(( (struct sockaddr_in *)&ifr.ifr_addr )->sin_addr) );
        // End IP address for wlan0

        // MAC address for wlan0
        strcpy(s.ifr_name, "wlan0");

        if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
        int i;
        for (i = 0; i < 6; ++i)
        printf(" %02x", (unsigned char) s.ifr_addr.sa_data[i]);
        puts("\n");
        }
        // End MAC address for wlan0
        
        // IP address for lo
        char array2[] = "lo";

        n = socket(AF_INET, SOCK_DGRAM, 0);

        //Type of address to retrieve - IPv4 IP address

        ifr.ifr_addr.sa_family = AF_INET;

        //Copy the interface name in the ifreq structure

        strncpy(ifr.ifr_name , array2 , IFNAMSIZ - 1);

        ioctl(n, SIOCGIFADDR, &ifr);

        close(n);

        //display result

        printf("IP Address of %s: %s and MAC address: " , array2 , inet_ntoa(( (struct sockaddr_in *)&ifr.ifr_addr )->sin_addr) );
        // End IP address for lo
        
        // MAC address for lo 
        strcpy(s.ifr_name, "lo");
        
        if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
        int i;
        for (i = 0; i < 6; ++i)
        printf(" %02x", (unsigned char) s.ifr_addr.sa_data[i]);
        puts("\n");
        }
        // End MAC address for lo
        return 0;

    }
IP & MAC address retrieval.

Comments are closed.