#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操作Microsoft Office(Word、Excel、PowerPoint)

    POI 与 Microsoft Office 1. POI 简介 POI 是 Apache 下的 Jakata 项目的一个子项目,主要用于提供 java 操作 Microsoft Office 办公套 ...

  2. django初体检课程

    最简便的Python web开发框架. C:\Windows\System32>E: E:\>django-admin startproject mysite E:\>cd mysi ...

  3. iOS学习之UINavigationController详解与使用(三)ToolBar

    1.显示Toolbar  在RootViewController.m的- (void)viewDidLoad方法中添加代码,这样Toobar就显示出来了. [cpp] view plaincopy [ ...

  4. addin1

    Mono.addin是一个插件框架,更多信息请访问 http://monoaddins.codeplex.com/

  5. css常用属性总结之 id和class的区别,使用类还是ID?

    前面两篇文章我们分别谈到了class和id的相关知识和如何使用,但是在实际项目中,我们该如何抉择,class还是id? 先回顾下两者的区别吧! 1.id具有唯一性,class具有普遍性,所以一个页面同 ...

  6. 蓝桥杯算法训练 java算法 表达式求值

    问题描述 输入一个只包含加减乖除和括号的合法表达式,求表达式的值.其中除表示整除. 输入格式 输入一行,包含一个表达式. 输出格式 输出这个表达式的值. 样例输入 1-2+3*(4-5) 样例输出 - ...

  7. Thinking in 查询设计

    近日,互联网动物园的各位小伙伴们召开了一次会议,考虑到大火的电子商务,准备在动物园里开发一个电商系统.首先上台的是销售山鸡,清了清嗓子,说道,人类正在进行电商革命,动物园也需要上一个电商系统,必要性有 ...

  8. CloudStack 安装时需要的第三方包

    1.mysql-connector-java 2.jakarta-commons-daemon-jsvc 3.jsvc   rpm -ivh cloudstack-common-4.1.1-0.el6 ...

  9. RTX二次开发笔记1

    在客户端,一个rtx用户给另一个rtx用户发送消息! 我的解决方案:在rtx服务端部署一个 wcf服务 或 webservice 服务,然后程序在客户端调用服务就行. 1,C#版 (服务端需要4个DL ...

  10. Luogu 4725 【模板】多项式对数函数

    继续补全模板. 要求 $$g(x) = ln f(x)$$ 两边求导, $$g'(x) = \frac{f'(x)}{f(x)}$$ 然后左转去把多项式求导和多项式求逆的模板复制过来,就可以计算出$g ...