#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
void get_ip(const char *mac,char *ip)
{
char temp_cmd[32] = "cat arp.txt";
char temp_ip[32] = {0};
char temp_buff[128] = {0};
char temp_mac[32] = {0};
FILE *temp_fp = popen(temp_cmd,"r");
if(temp_fp == NULL)
{
perror("temp_fp open error!\n");
exit(1);
}
while(fgets(temp_buff,sizeof(temp_buff),temp_fp) != NULL)
{
memset(temp_ip,0,sizeof(temp_ip));
memset(temp_mac,0,sizeof(temp_mac));
sscanf(temp_buff,"%s %*s %s %*s",temp_ip,temp_mac);
/*printf("-------------arp -----------\n");
printf("the temp_ip is %s\n",temp_ip);
printf("the temp_mac is %s\n",temp_mac);
printf("-------------end-------------\n");*/
if(!strncmp(temp_mac,mac,strlen(mac)))
{
//printf("------ok----------\n");
//printf("the ip is %s\n",temp_ip);
strncpy(ip,temp_ip,strlen(temp_ip));
}
}
pclose(temp_fp);
}
//正确的时候同一组数据进行写入,需要时事处理,循环事件,文件开始的创建,三分钟的刷新事件,
//通过事件进行出发
void get_macAndiface()
{
char buff_cmd[32] = "cat mac.txt";
char buff_iface[12] = {0};
char buff_mac[32] = {0};
char buff_flag[12] = {0};
char buff_time[18] = {0};
char buff_temp[128] = {0};
//FILE *file_fp = fopen("/tmp/tdhcp","w");
FILE *file_fp = fopen("tdhcp","w");
if(file_fp == NULL)
{
perror("file_fp error!\n");
exit(1);
}
char *buff_ip =(char *)malloc(32);
if(buff_ip == NULL)
{
perror("buff_ip erorr!\n");
exit(1);
}
FILE *fp =popen(buff_cmd,"r");
if(fp == NULL)
{
perror("fd open error!\n");
exit(1);
}
while(fgets(buff_temp,sizeof(buff_temp),fp) != NULL)
{
//buff 至空操作
memset(buff_iface,0,sizeof(buff_iface));
memset(buff_mac,0,sizeof(buff_mac));
memset(buff_flag,0,sizeof(buff_flag));
memset(buff_time,0,sizeof(buff_time));
sscanf(buff_temp,"%s %s %s %s",buff_iface,buff_mac,buff_flag,buff_time);
if(!strncmp(buff_iface,"ra0",3) || !strncmp(buff_iface,"rai0",4))
{
if(!strncmp(buff_flag,"no",2))
{
get_ip(buff_mac,buff_ip);
printf("the wireless buff_ip is %s\n",buff_ip);
/***数据写入文件,追加写 位置 /temp/dhcp***/
fprintf(file_fp,"%s\n",buff_iface);
fprintf(file_fp,"%s\n",buff_mac);
fprintf(file_fp,"%s\n",buff_ip);
fprintf(file_fp,"%s\n",buff_time);
}
}
}
fclose(file_fp);
pclose(fp);
free(buff_ip);
buff_ip = NULL;
}
int main()
{
get_macAndiface();
return 0;
}
liyong@ubuntu:~/c/get_addr$ cat mac.txt
interface mac addr is local? ageing timer
eth0.1 00:0e:c6:46:0f:0c no 29.93
eth1 00:a4:c5:11:22:33 yes 0.00
rai0 02:a4:c5:11:22:33 yes 0.00
ra0 fc:a5:d0:43:9f:0d no 0.82
liyong@ubuntu:~/c/get_addr$ cat arp.txt
Address HWtype HWaddress Flags Mask Iface
192.168.1.3 (incomplete) br0
192.168.1.2 ether fc:a5:d0:43:9f:0d C br0
192.168.1.10 ether 00:0e:c6:46:0f:0c C br0