Last active
September 27, 2022 01:35
-
-
Save keeler/8345633 to your computer and use it in GitHub Desktop.
View IP and MAC addresses of devices on your LAN. Response to http://stackoverflow.com/questions/21031755/linux-c-how-to-programatically-get-mac-address-for-all-adapters-on-a-lan.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int main(int argc, char** argv) | |
{ | |
const int size = 256; | |
char ip_address[size]; | |
int hw_type; | |
int flags; | |
char mac_address[size]; | |
char mask[size]; | |
char device[size]; | |
FILE* fp = fopen("/proc/net/arp", "r"); | |
if(fp == NULL) | |
{ | |
perror("Error opening /proc/net/arp"); | |
} | |
char line[size]; | |
fgets(line, size, fp); // Skip the first line, which consists of column headers. | |
while(fgets(line, size, fp)) | |
{ | |
sscanf(line, "%s 0x%x 0x%x %s %s %s\n", | |
ip_address, | |
&hw_type, | |
&flags, | |
mac_address, | |
mask, | |
device); | |
printf("IP = %s, MAC = %s", ip_address, mac_address); | |
} | |
fclose(fp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment