Linux環境下擷取當前主機的IP/MAC等資訊(C/C++),linuxmac

來源:互聯網
上載者:User

Linux環境下擷取當前主機的IP/MAC等資訊(C/C++),linuxmac

本文介紹在unix環境下擷取當前主機IP,MAC地址等網路介面資訊。在不確定裝置網卡類型的情況下,通過預先定義幾種可能的網卡類型,然後依次遍曆可能的裝置如eth0,eth1,藉助ioctl函數擷取主機的全部網路介面資訊。

ioctl函數可以擷取所在主機的全部網路介面資訊,包括介面地址(IP,MAC,MASK…)、是否支援廣播等。ioctl函數的標頭檔沒有標準化,許多系統在“unistd.h中定義,而傳統的BSD系統中,通常在 “sys/ioctl.h”中定義 。ioctl的原型為:?
int ioctl(int fd,int request, void *arg );?
返回:成功返回0,失敗返回-1?
參數fd通常是擷取介面資訊所用到的控制代碼,在本文擷取ip等資訊時,是socket串連控制代碼;?
參數arg總是一個指標,其類型依賴於request參數;?
request類型主要分為6類:通訊端操作、檔案操作、介面操作、ARP快取操作、路由表操作、流系統?
本文要介紹的擷取IP地址和MAC等屬於介面操作。其中?
IP地址的request類型為SIOCGIFADDR?
MAC地址的request類型為SIOCGIFHWADDR?
其餘類型參考文獻[1],第17章相關章節。

有關請求參數struct ifreq的定義為位於“net/if.h”

/* Interface request structure used for socket ioctl's.  All interface   ioctl's must have parameter definitions which begin with ifr_name.   The remainder may be interface specific.  */struct ifreq  {# define IFHWADDRLEN    6# define IFNAMSIZ   IF_NAMESIZE    union      {    char ifrn_name[IFNAMSIZ];   /* Interface name, e.g. "en0".  */      } ifr_ifrn;    union      {    struct sockaddr ifru_addr;    struct sockaddr ifru_dstaddr;    struct sockaddr ifru_broadaddr;    struct sockaddr ifru_netmask;    struct sockaddr ifru_hwaddr;    short int ifru_flags;    int ifru_ivalue;    int ifru_mtu;    struct ifmap ifru_map;    char ifru_slave[IFNAMSIZ];  /* Just fits the size */    char ifru_newname[IFNAMSIZ];    __caddr_t ifru_data;      } ifr_ifru;  };# define ifr_name   ifr_ifrn.ifrn_name  /* interface name   */# define ifr_hwaddr ifr_ifru.ifru_hwaddr    /* MAC address      */# define ifr_addr   ifr_ifru.ifru_addr  /* address      */# define ifr_dstaddr    ifr_ifru.ifru_dstaddr   /* other end of p-p lnk */# define ifr_broadaddr  ifr_ifru.ifru_broadaddr /* broadcast address    */# define ifr_netmask    ifr_ifru.ifru_netmask   /* interface net mask   */# define ifr_flags  ifr_ifru.ifru_flags /* flags        */# define ifr_metric ifr_ifru.ifru_ivalue    /* metric       */# define ifr_mtu    ifr_ifru.ifru_mtu   /* mtu          */# define ifr_map    ifr_ifru.ifru_map   /* device map       */# define ifr_slave  ifr_ifru.ifru_slave /* slave device     */# define ifr_data   ifr_ifru.ifru_data  /* for use by interface */# define ifr_ifindex    ifr_ifru.ifru_ivalue    /* interface index      */# define ifr_bandwidth  ifr_ifru.ifru_ivalue    /* link bandwidth   */# define ifr_qlen   ifr_ifru.ifru_ivalue    /* queue length     */# define ifr_newname    ifr_ifru.ifru_newname   /* New name     */# define _IOT_ifreq _IOT(_IOTS(char),IFNAMSIZ,_IOTS(char),16,0,0)# define _IOT_ifreq_short _IOT(_IOTS(char),IFNAMSIZ,_IOTS(short),1,0,0)# define _IOT_ifreq_int _IOT(_IOTS(char),IFNAMSIZ,_IOTS(int),1,0,0)
  •  

  • 下面是相關代碼:

    #include #include #include #include #include #include #include #include #include #include using namespace std;bool GetIP(const vector& vNetType,string& strip){    for(size_t i=0;isin_addr);            if(!strip.empty())            {                ::close(fd);                return true;            }        }    }    return false;}int main(){    vector vs;//預先定義了幾種可能的網卡類型    vs.push_back("eth");    vs.push_back("em");    vs.push_back("oct");    string str;    if( (GetIP(vs,str)) )    {        cout<<
    • 擷取MAC地址的程式碼片段如下:

      擷取MAC地址的代碼如下fd = socket(AF_INET, SOCK_DGRAM, 0);strcpy(ifr.ifr_name, strDevice.c_str() );if (ioctl(fd, SIOCGIFHWADDR, &ifr) <  0){    ::close(fd);    continue;}unsigned char macaddr[6]; memcpy(macaddr,ifr.ifr_hwaddr.sa_data,6);  for(int k=0;k<6;k++)  {    printf("%02x",macaddr[k]);    if(k<5)        printf(":");}
    <

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.