linux 擷取ip地址 MAC地址 netMask地址

來源:互聯網
上載者:User

linux下擷取指定網路的ip地址與netmask的介面:

int get_ip(char* ipaddr,char *netmask, const char *ethname )
{
    int ret=-1;
    if ( ipaddr && netmask && ethname )
    {
          int fd, intrface;
          struct ifreq buf[16];
          struct ifconf ifc;
  
          if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
               return -1;
          ifc.ifc_len = sizeof(buf);
          ifc.ifc_buf = (caddr_t)buf;
   
          if (ioctl (fd, SIOCGIFCONF, (char *) &ifc) < 0)
                goto _error_;
  
          intrface = ifc.ifc_len/sizeof(struct ifreq);
  
          while(intrface-->0)
          {
                if (strstr(buf[intrface].ifr_name, ethname))
               {
                        if ((ioctl (fd, SIOCGIFADDR, (char*)&buf[intrface])) < 0)
                                goto _error_;
                        sprintf(ipaddr, "%s", 
                                 inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr));
    
                        if ((ioctl (fd, SIOCGIFNETMASK , (char*)&buf[intrface])) < 0)
                                goto _error_;

                        sprintf(netmask, "%s", 
                            inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_netmask))->sin_addr));
                        ret = 0;
               }
          }
  
        _error_:
           close(fd);
    }
 
    return ret;
}

 

擷取mac地址的介面:

int get_mac(char *mac)
{
     int fd, ret=-1;
     if ( mac )
     {
           struct ifreq ifr;
  
           if( (fd = socket(AF_INET, SOCK_DGRAM, 0) )<0)
          {
                 perror("socket");
                 return -1;
          }
 
          ifr.ifr_addr.sa_family = AF_INET;
          strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1); 
 
          if(ioctl(fd, SIOCGIFHWADDR, &ifr) == 0)
          {  
              sprintf(mac, "%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]);
 
               strupr(mac);
   
               ret = 0;
         }
  
         close(fd);
    }
 
    return ret;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.