需求:读取/proc/net/arp文件的ip和mac信息
#include <stdio.h>
#include <unistd.h>
char *arp_get()
{
FILE *proc;
char ip[16];
char mac[18];
if (!(proc = fopen("/proc/net/arp", "r"))) {
return NULL;
}
/* Skip first line */
while (!feof(proc) && fgetc(proc) != '\n');
/* Find ip, copy mac in reply */
while (!feof(proc) && (fscanf(proc, " %15[0-9.] %*s %*s %17[A-Fa-f0-9:] %*s %*s", ip, mac) == 2)) {
printf("ip=%s, mac=%s\n", ip, mac);
}
fclose(proc);
return NULL;
}
int main(void)
{
arp_get();
return 0;
}