日期:2014-05-16  浏览次数:20666 次

unix、linux下只输出本机mac地址的命令
unix、linux下只输出本机mac地址的shell命令?
不要ifconfig那种的输出一大堆。。。最好时只输出mac字符串

------解决方案--------------------
# ifconfig  
------解决方案--------------------
 grep HWaddr 
------解决方案--------------------
 awk '{print $4,$5}'
HWaddr 00:50:56:8E:00:3B
HWaddr 52:54:00:B8:7D:FA
#

------解决方案--------------------
ifconfig 
------解决方案--------------------
 awk '{print $5}' 
------解决方案--------------------
 head -1
------解决方案--------------------
我想已经猜到楼主意欲何为了。。。
楼主还是直接用ioctl吧

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>

int getmac(char *ethname, char *macbuf)
{
        int sock;
        struct ifreq iface;
        char tmp_mac[10];

        sock = socket(PF_INET, SOCK_DGRAM, 0);
        strcpy(iface.ifr_name, ethname);

        if(ioctl(sock, SIOCGIFHWADDR, &iface) != 0) {
                close(sock);
                return 1;
        }

        memcpy(tmp_mac, iface.ifr_hwaddr.sa_data, 6);
        sprintf(macbuf, "%02X:%02X:%02X:%02X:%02X:%02X",
                tmp_mac[0] & 0xFF, tmp_mac[1] & 0xFF, tmp_mac[2] &0xFF,