首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

unix、linux停只输出本机mac地址的命令

2013-08-04 
unix、linux下只输出本机mac地址的命令unix、linux下只输出本机mac地址的shell命令?不要ifconfig那种的输出

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,


                tmp_mac[3] & 0xFF, tmp_mac[4] & 0xFF, tmp_mac[5] & 0xFF);
        return 0;
}

int main(void)
{
        char buf[256];

        getmac("eth0", buf);
        puts(buf);

        return 0;
}


[解决办法]
没用过苹果系统,不过我觉得上面的资料已经足够了。
Shell方式,可以根据1、2楼的写法自己改。查查grep和awk基本用法而已。
我的方法,如果用苹果系统有strace,那么跟踪一下ifconfig在获取网卡MAC是的syscall。看看是否确如我提供的那个ioctl。如果是,google苹果系统下这个API的头文件;如果不是,用ifconfig的那个syscall。

热点排行