Linux 下获取网络属性(ip,mac)

  1. 指令获取

显示当前系统拥有的网卡

# ls /sys/class/net/                                               
eth0  ip6tnl0  lo  p2p0  sit0  wlan0  

显示网卡eth0 当前的属性节点文件

#ls  /sys/class/net/eth0/                                         
addr_assign_type  dormant            mtu               proto_down    
addr_len          duplex             name_assign_type  queues        
address           flags              netdev_group      speed         
broadcast         gro_flush_timeout  operstate         statistics    
carrier           ifalias            phys_port_id      subsystem     
carrier_changes   ifindex            phys_port_name    tx_queue_len  
dev_id            iflink             phys_switch_id    type          
dev_port          link_mode          power             uevent   

显示网卡mac地址

# cat  /sys/class/net/eth0/address                                 
b0:d5:68:bb:be:53
# more /sys/class/net/eth0/address                                 
b0:d5:68:bb:be:53

更完整的显示方式

# ifconfig  eth0                                                   
eth0      Link encap:Ethernet  HWaddr b0:d5:68:bb:be:53
          inet addr:192.168.3.20  Bcast:192.168.3.255  Mask:255.255.255.0 
          inet6 addr: fe80::b2d5:68ff:febb:be53/64 Scope: Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:7531 errors:0 dropped:14 overruns:0 frame:0 
          TX packets:7261 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1000 
          RX bytes:8886959 TX bytes:1266917 
          Interrupt:41 Base address:0x4000 
  1. 代码实现

获取eth0 mac 地址

int QueryEtheHwMac()
{
    int sock_mac;
    struct ifreq ifr_mac;
    char mac_addr[32] = {0x00};

    sock_mac = socket(AF_INET, SOCK_STREAM, 0);
    if(sock_mac == -1)
    {
        printf("create socket failed!\n");
        return 0;
    }

    memset(&ifr_mac, 0x00, sizeof(ifr_mac));
    strncpy(ifr_mac.ifr_name, "eth0", sizeof(ifr_mac.ifr_name)-1);

    if((ioctl(sock_mac, SIOCGIFHWADDR, &ifr_mac)) < 0)
    {
        return 0;
    }

    sprintf(mac_addr, "%02x:%02x:%02x:%02x:%02x:%02x",\
        (unsigned char)ifr_mac.ifr_hwaddr.sa_data[0],  \
        (unsigned char)ifr_mac.ifr_hwaddr.sa_data[1],  \
        (unsigned char)ifr_mac.ifr_hwaddr.sa_data[2],  \
        (unsigned char)ifr_mac.ifr_hwaddr.sa_data[3],  \
        (unsigned char)ifr_mac.ifr_hwaddr.sa_data[4],  \
        (unsigned char)ifr_mac.ifr_hwaddr.sa_data[5]);

    NETDET(printf("the eth0 mac address: %s .\n",mac_addr);)
    return 1;
}

创建SOCK_STREAM类型socket, ioctl 获取SIOCGIFHWADDR 属性即可获取网卡的mac 地址,此处ifr_mac.ifr_name设置的是eth0, 那么获取的地址也就是eth0 的mac地址。

获取eth0 ip地址
相当于将上面的SIOCGIFHWADDR 改成SIOCGIFADDR 就可以获取到IP地址信息

int QueryEth0Ip()
{
    int sock_ip;
    char ipaddr[32];
    struct ifreq ifr_ip;
    struct   sockaddr_in *sin;
    char mac_addr[32] = {0x00};

    sock_ip = socket(AF_INET, SOCK_STREAM, 0);
    if(sock_ip == -1)
    {
        printf("create socket failed!\n");
        return 0;
    }

    memset(&ifr_ip, 0x00, sizeof(ifr_mac));
    strncpy(ifr_ip.ifr_name, "eth0", sizeof(ifr_ip.ifr_name)-1);

    if((ioctl(sock_ip, SIOCGIFADDR, &ifr_ip)) < 0)
    {
        printf("get eth0 ip failed !\n");
        return 0;
    }
	sin = (struct sockaddr_in *)&ifr_ip.ifr_addr; 
	strcpy(ipaddr,inet_ntoa(sin->sin_addr));
	printf("eth0 ip: %s \n",ipaddr);
    close( sock_ip ); 
    return 1;
}

获取link 状态

int getEthernetLinkState()
{
    int skfd = 0;
    struct ifreq ifr;

    skfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(skfd < 0) 
    {
        printf("%s:%d Open socket error!\n", __FILE__, __LINE__);
        return 0;
    }

    strcpy(ifr.ifr_name, "eth0");

    if(ioctl(skfd, SIOCGIFFLAGS, &ifr) <0 )
    {
        printf("%s:%d IOCTL error!\n", __FILE__, __LINE__);
        printf("Maybe ethernet inferface %s is not valid!", ifr.ifr_name);
        close(skfd);
        return 0;
    }

    if(ifr.ifr_flags & IFF_RUNNING)
    {
        close(skfd);
        return 1;
    }
    else
    {
        close(skfd);
        return 0;
    }
}

其它的子网掩码,广播地址可以通过修改ioctl 的第二个参数为SIOCGIFNETMASK, SIOCGIFBRDADDR即可得到。

不熟悉socket 编程的朋友可以通过system,exec 封装shell 指令,然后解析可以得到这些数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值