1. 程式人生 > >C語言獲取本地所有網絡卡的ip地址及MAC資訊

C語言獲取本地所有網絡卡的ip地址及MAC資訊

  本程式在ubuntu下執行成功,後再centos系統下也能編譯成功並執行。原始碼如下
  

// demo01.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include <netdb.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h> #define MAC_SIZE 18 #define IP_SIZE 16 // function declare int get_ip_by_domain(const char *domain, char *ip); // 根據域名獲取ip int get_local_mac(const char *eth_inf, char *mac); // 獲取本機mac int get_local_ip(const char *eth_inf, char *ip); // 獲取本機ip /****** main test **********/
int main(void) { char ip[IP_SIZE]; char mac[MAC_SIZE]; //get all the information of interface int sockfd; struct ifconf ifconf; struct ifreq *ifreq; char buf[512]; ifconf.ifc_len=512; ifconf.ifc_buf=buf; if( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) { perror("socket"
); exit(1); } ioctl(sockfd, SIOCGIFCONF, &ifconf); ifreq = (struct ifreq*)ifconf.ifc_buf; int i=0; printf(" local ip&mac information\n"); printf("=======================================================================\n"); printf(" name ip mac \n"); for(i=(ifconf.ifc_len/sizeof(struct ifreq)); i>0; i--) { if(ifreq->ifr_flags == AF_INET) { get_local_mac(ifreq->ifr_name, mac); get_local_ip(ifreq->ifr_name, ip); printf(" %-10s %-20s %-20s \n",ifreq->ifr_name, ip, mac); ifreq++; } } printf("=======================================================================\n"); return 0; } // get the ip address by domain int get_ip_by_domain(const char *domain, char *ip) { char **pptr; struct hostent *hptr; hptr = gethostbyname(domain); if(NULL == hptr) { printf("gethostbyname error for host:%s/n", domain); return -1; } for(pptr = hptr->h_addr_list ; *pptr != NULL; pptr++) { if (NULL != inet_ntop(hptr->h_addrtype, *pptr, ip, IP_SIZE) ) { return 0; // 只獲取第一個 ip } } return -1; } // get the local host mac address int get_local_mac(const char *eth_inf, char *mac) { struct ifreq ifr; int sd; bzero(&ifr, sizeof(struct ifreq)); if( (sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("get %s mac address socket creat error\n", eth_inf); return -1; } strncpy(ifr.ifr_name, eth_inf, sizeof(ifr.ifr_name) - 1); if(ioctl(sd, SIOCGIFHWADDR, &ifr) < 0) { printf("get %s mac address error\n", eth_inf); close(sd); return -1; } snprintf(mac, MAC_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x", (unsigned char)ifr.ifr_hwaddr.sa_data[0], (unsigned char)ifr.ifr_hwaddr.sa_data[1], (unsigned char)ifr.ifr_hwaddr.sa_data[2], (unsigned char)ifr.ifr_hwaddr.sa_data[3], (unsigned char)ifr.ifr_hwaddr.sa_data[4], (unsigned char)ifr.ifr_hwaddr.sa_data[5]); close(sd); return 0; } // get the local host ip address int get_local_ip(const char *eth_inf, char *ip) { int sd; struct sockaddr_in sin; struct ifreq ifr; sd = socket(AF_INET, SOCK_DGRAM, 0); if (-1 == sd) { printf("socket error: %s\n", strerror(errno)); return -1; } strncpy(ifr.ifr_name, eth_inf, IFNAMSIZ); ifr.ifr_name[IFNAMSIZ - 1] = 0; // if error: No such device if (ioctl(sd, SIOCGIFADDR, &ifr) < 0) { printf("ioctl error: %s\n", strerror(errno)); close(sd); return -1; } memcpy(&sin, &ifr.ifr_addr, sizeof(sin)); snprintf(ip, IP_SIZE, "%s", inet_ntoa(sin.sin_addr)); close(sd); return 0; }

編譯指令碼如下:

#This is the makefile of EpollTest
.PHONY:all
all:demo01
server:
    gcc demo01.c -o demo01
clean:
    rm -f demo01

結果如下:

centos下執行結果圖