openwrt 显示客户端client 连接的信息 /proc/net/arp /tmp/dhcp.leases

显示当前路由器上,client的name,ip,mac,connect_type信息

有两种方式:来自链接
一。通过DHCP client分配列表 (缺点:client列表会根据超时时间刷新,一般超时时间为12h,)
二。通过arp缓存列表/proc/net/arp(缺点:arp刷新时间默认为30s,这个时间的误差可以接受。)

主要讲解第二种方法:
Flags: 0x0是找不到这台主机了。
0x2是主机还在线。

root@OpenWrt:/# cat /proc/net/arp 
IP address       HW type     Flags       HW address            Mask     Device
192.168.0.188    0x1         0x2         00:e0:4c:68:00:03     *        br-lan
192.168.0.30     0x1         0x2         28:ed:6a:e9:ab:fb     *        br-lan
root@OpenWrt:/# 
root@OpenWrt:/# cat /tmp/dhcp.leases 
1525722001 28:ed:6a:e9:ab:fb 192.168.0.30 lin-bo-de 01:28:ed:6a:e9:ab:fb
root@OpenWrt:/# 

connect_type
判断是否是通过WIFI链接的,从iwpriv ra0 show stacountinfo把mac记录下,对比mac是否存在,存在的话是通过WiFi链接的,否则的话不是

dmesg -c  表示显示信息后,清除ring buffer中的内容。

char *strupr(char *str)
{
    char *orign=str;
    for (; *str!='\0'; str++)
        *str = toupper(*str);
    return orign;
}


int IsWifi(char *MAC){
    char wifimac[BUF_SIZE_128]={0};
    char uppermac[BUF_SIZE_128]={0};
    char *commond1 = "dmesg -c|iwpriv ra0 show stacountinfo" ;
    char *commond2 = "dmesg | grep MAC -A 20 | awk '{print $3}' >/tmp/wifista.txt" ;
    system(commond1);   
    system(commond2);
    FILE* fd = fopen("/tmp/wifista.txt","r");
    if(fd < 0)
    {
        return -1;
    }
    strcpy(uppermac,strupr(MAC));
    while(fgets(wifimac, sizeof(wifimac), fd) != NULL)
    {
        if(strncmp(uppermac,wifimac,17) == 0)
            {
                my_printf(1, "%s(%d) COMPARE SUCCESED wifiMAC is %s uppermac is %s \n",__FUNCTION__,__LINE__,wifimac,uppermac);
                return TRUE;
            }

            my_printf(1, "%s(%d) wifiMAC is %s mac is %s \n",__FUNCTION__,__LINE__,wifimac,uppermac);
        }

}

MAC , IP, NAME
分割cat /tmp/dhcp.leases ,获取

int get_net_arp_list(char* dst_arp_list)
{
    FILE *f=NULL;
    //char buf[BUF_SIZE_1024] = {0};
    //char *buf = NULL;
    //buf = (char*)malloc(sizeof(char)*1024);
    if ((f = fopen("/proc/net/arp", "r")) != NULL)
    {
        fread(dst_arp_list,1,sizeof(char)*1024,f);
        //buffer = buf;
        //dst_dhcp_list = buf;
        my_printf(LOG_MODE_LEVEL_1, "%s(%d) dst_arp_list is %s \n",__FUNCTION__,__LINE__,dst_arp_list);
        fclose(f);
    }
    return 0;
}

你可能感兴趣的:(openwrt,linux)