Get IP address, mac address of all interface
C/C++ Way
This code is available for all Linux distributions even though I tested on the Ubuntu 18.04.
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <unistd.h> #include <string.h> #include <string> #include <sys/types.h> #include <ifaddrs.h> #include <linux/if_packet.h> #include <net/ethernet.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <net/if.h> #include <sys/ioctl.h> using namespace std; unsigned char * getMACAddress(const char *pinterface) { static unsigned char mac[8]; int sock = socket(PF_INET, SOCK_DGRAM, 0); struct ifreq req; int i = 0; memset(&req, 0, sizeof(req)); strncpy(req.ifr_name, pinterface, IF_NAMESIZE - 1); if (ioctl(sock, SIOCGIFHWADDR, &req) < 0) { perror("ioctl"); exit(EXIT_FAILURE); } memcpy(&mac, req.ifr_hwaddr.sa_data, 6); mac[6] = 0x00; /* for(i=0;i<6;i++) { printf("%.2X", (unsigned char) req.ifr_hwaddr.sa_data[i]); if (i < 5) printf(":"); } printf("\n"); */ close(sock); return mac; } const char *printMac(unsigned char *mac){ static char address[32]; sprintf(address, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); return address; } string getIPAddress(){ string ipAddress="Unable to get IP Address"; struct ifaddrs *interfaces = NULL; struct ifaddrs *temp_addr = NULL; int success = 0; unsigned char mac[8]; int i; // retrieve the current interfaces - returns 0 on success success = getifaddrs(&interfaces); if (success == 0) { // Loop through linked list of interfaces temp_addr = interfaces; while(temp_addr != NULL) { if(temp_addr->ifa_addr->sa_family == AF_INET) { memcpy(mac ,getMACAddress(temp_addr->ifa_name) , 6); ipAddress=inet_ntoa(((struct sockaddr_in*)temp_addr->ifa_addr)->sin_addr); cout << "interface name:" << temp_addr->ifa_name << " MAC:" << printMac(mac)<< " IP Address:" << ipAddress.c_str() <<endl; } temp_addr = temp_addr->ifa_next; } } // Free memory freeifaddrs(interfaces); return ipAddress; } int main(int argc, char *argv[]) { getIPAddress(); return 0; }
You don't need to link any library. Hust compile the code.
g++ main.cpp
Now run the code. You can get the mac address, ip address of all the interfaces.
root@spypiggy-ubuntu:/usr/local/src# ./a.out interface name:lo MAC:00:00:00:00:00:00 IP Address:127.0.0.1 interface name:enp4s0 MAC:E0:D5:5E:E6:1A:3E IP Address:192.168.0.220
Python Way
Several methods are possible, but the psutil module is the most convenient.
First, install psutil. To install psutil, you must have python-dev and build-essentials installed.
[root@tone-ivr2 dialplan]#apt-get install python3-dev build-essentials [root@tone-ivr2 dialplan]#pip3 install psutil
If you use psutil, you can get interface information and IP address by using the following simple code.
import psutil import socket def get_interface_info(): results = [] for interface in sorted(psutil.net_if_addrs().keys()): ip_address = "" mac_address = "" for addr in psutil.net_if_addrs()[interface]: if addr.family == socket.AF_INET: ip_address = addr.address if addr.family == psutil.AF_LINK: mac_address = addr.address results.append({"id": interface, "name": interface, "ip_address": ip_address,"mac_address": mac_address}) return results results = get_interface_info() for result in results: print('===============================') print('ID:%s'%result["id"]) print('NAME:%s'%result["name"]) print('IP:%s'%result["ip_address"]) print('MAC:%s'%result["mac_address"]) print('===============================')
If you run the code you will see that it gets the IP and MAC information for all interfaces.
[root@tone-ivr2 mac]# python3 mac.py interface[eno1] MAC Address : ac:1f:6b:f6:28:3a interface[eno2] MAC Address : ac:1f:6b:f6:28:3b interface[lo] MAC Address : 00:00:00:00:00:00 =============================== ID:eno1 NAME:eno1 IP:221.139.49.54 MAC:ac:1f:6b:f6:28:6a =============================== ID:eno2 NAME:eno2 IP: MAC:ac:1f:6b:f6:28:6b =============================== ID:lo NAME:lo IP:127.0.0.1 MAC:00:00:00:00:00:00 ===============================
댓글
댓글 쓰기