获取mac接口下对应的IP

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘多拉的面

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值