#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>

#define MAXINTERFACES 16

void get_ip()
{
    int sock_fd;
    struct ifreq buf[MAXINTERFACES];
    struct ifconf ifc;
    int interface_num;
    char *addr;//[ADDR_LEN];

if((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        printf("Create socket failed");
        return;
    }
    ifc.ifc_len = sizeof(buf);
    ifc.ifc_req = buf;
    if(ioctl(sock_fd, SIOCGIFCONF, (char *)&ifc) < 0)
    {
        printf("Get a list of interface addresses failed");
        return;
    }
    
    interface_num = ifc.ifc_len / sizeof(struct ifreq);
    printf("The number of interfaces is %d\n", interface_num);

while(interface_num--) 
    {
        printf("Net device: %s\n", buf[interface_num].ifr_name);

if(ioctl(sock_fd, SIOCGIFFLAGS, (char *)&buf[interface_num]) < 0)
        {
            printf("Get the active flag word of the device");
            continue;
        }
        if(buf[interface_num].ifr_flags & IFF_PROMISC)
            printf("Interface is in promiscuous mode\n");

if(buf[interface_num].ifr_flags & IFF_UP)
            printf("Interface is running\n");
        else
            printf("Interface is not running\n");

if(ioctl(sock_fd, SIOCGIFADDR, (char *)&buf[interface_num]) < 0)
        {
            printf("Get interface address failed");
            continue;
        }
        addr = inet_ntoa(((struct sockaddr_in*)(&buf[interface_num].ifr_addr))->sin_addr);
        printf("IP address is %s\n", addr);  
    }

close(sock_fd);
    return;
}

void get_mac()
{
    int sock_fd;
    struct ifreq buf[MAXINTERFACES];
    struct ifconf ifc;
    int interface_num;
    
    if((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        printf("Create socket failed");
        return;
    }
    ifc.ifc_len = sizeof(buf);
    ifc.ifc_req = buf;
    if(ioctl(sock_fd, SIOCGIFCONF, (char *)&ifc) < 0)
    {
        printf("Get a list of interface addresses failed");
        return;
    }
    interface_num = ifc.ifc_len / sizeof(struct ifreq);
    printf("The number of interfaces is %d\n", interface_num);

while(interface_num--) 
    {
        printf("Net device: %s\n", buf[interface_num].ifr_name);

if(ioctl(sock_fd, SIOCGIFFLAGS, (char *)&buf[interface_num]) < 0)
        {
            printf("Get the active flag word of the device");
            continue;
        }
        if(buf[interface_num].ifr_flags & IFF_PROMISC)
            printf("Interface is in promiscuous mode\n");

if(buf[interface_num].ifr_flags & IFF_UP)
            printf("Interface is running\n");
        else
            printf("Interface is not running\n");

if(ioctl(sock_fd, SIOCGIFHWADDR, (char *)&buf[interface_num]) < 0)
        {
            printf("Get the hardware address of a device failed");
            continue;
        }

printf("Mac address is: %02X:%02X:%02X:%02X:%02X:%02X\n",
            (unsigned char)buf[interface_num].ifr_hwaddr.sa_data[0],
            (unsigned char)buf[interface_num].ifr_hwaddr.sa_data[1],
            (unsigned char)buf[interface_num].ifr_hwaddr.sa_data[2],
            (unsigned char)buf[interface_num].ifr_hwaddr.sa_data[3],
            (unsigned char)buf[interface_num].ifr_hwaddr.sa_data[4],
            (unsigned char)buf[interface_num].ifr_hwaddr.sa_data[5]);
    }

close(sock_fd);
}

Get MAC address using POSIX APIs的更多相关文章

  1. Ubuntu 下,修改 Mac address

    ifconfig    //    check Mac address sudo ifconfig eth0 down sudo ifconfig eth0 hw ether xx:xx:xx:xx: ...

  2. 【小错误】Device eth2 has different MAC address than expected, ignoring.

    今天在搭建rac配置IP的时候报错显示如下: Device eth2 has different MAC address than expected, ignoring.[FAILED] 百度了下,问 ...

  3. Find mac address

    Windows Method 1: Using the Command Prompt 1 Click on the Start button.   2 Type cmd in the search b ...

  4. OK335xS mac address hacking

    /*********************************************************************** * OK335xS mac address hacki ...

  5. 利用C语言获取设备的MAC address

    利用C语言获取设备的MAC address MAC address --> Medium Access Control layer address // // http://www.binary ...

  6. 关于获得本机Mac Address的方法

    网络上有讲获得Mac address的方法有如下: 1. 发送ARP命令,利用返回的Mac Address缓冲区得到 2. 用NetworkInterface.GetAllNetworkInterfa ...

  7. what is MAC address

    MAC Address:media access control address A media access control address (MAC address) is a unique id ...

  8. vb.net 使用ip查詢(Host Name)(WorkGroup Name)(MAC Address)-運用cmd及nbtstat命令

    Sub nbtstat(ByVal ip As String) Dim strRst, strRst1, strRst2, strRst3 As String Dim n1, n2, n3 As In ...

  9. How to Change MAC Address on Ubuntu

    1 Open Terminal.   2 Log in as root so type: sudo -i and then write your password.   3 View your cur ...

随机推荐

  1. poi操作word 2007 常用方法总结

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...

  2. 强大的vim配置文件,让编程更随意 (转载)

    花了很长时间整理的,感觉用起来很方便,共享一下. 我的vim配置主要有以下优点: 1.按F5可以直接编译并执行C.C++.java代码以及执行shell脚本,按“F8”可进行C.C++代码的调试 2. ...

  3. lucene3.0范围查找

    在lucene3.0以上版本中,范围查询也有很大的变化,RangeQuery已经不推荐使用,使用TermRangeQuery和NumericRangeQuery两个替代.TermRangeQuery: ...

  4. beego 自定义模板函数

    beego支持的模板函数不是很多,有时候前端展现数据的时候,要对数据进行格式化,所以要用到自定义模板函数 比如我的前端模板上有时间和模板大小这2个数据,原始数据都是int的时间戳和byte单位的数据, ...

  5. git 转移

    git push --mirror https://github.com/cloud-pi/drbd-docker-plugin.git

  6. 最近工作的一点小tips

    最近工作比较忙,但也积累了一些小tips,比较杂,不成系统,也并不很深入,就开一篇笼统的先记录一下,以后再深入挖掘. 1.-webkit-tap-highlight-color -webkit-tap ...

  7. EZOJ #224

    传送门 分析 首先我们发现要让答案最小,或运算一定是没有用的 我们还可以发现a^b = a&(~b) 所以异或运算也没有用 于是我们只考虑否和与 我们还会得到一个性质就是没增加一个数一定会让答 ...

  8. code1319 玩具装箱

    一个划分dp,不过由于划分个数任意,仅用一维数组就可以 设dp[i]表示前i个装箱(任意个箱子)的费用最小值 dp[i]=min(dp[u]+cost(u+1,i)) 但是n<=50000,n方 ...

  9. Ubuntu的SWAP设置

    1. 在Ubuntu中配置使用新创建的Swap分区 Command list: 查找Swap分区的UUID sudo blkid 在/ect/fstab中加入新的Swap分区 sudo gedit / ...

  10. jstack调试core文件

    摘自:https://stackoverflow.com/questions/37331266/jstack-throws-exception-interrogating-a-core // 错误示例 ...