Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

406
Views
Linux C++ How to Programatically Get MAC address for all adapters on a LAN

How may I use C or C++ PROGRAM (no command line) to get the MAC addresses (I'll take the IP addresses too if they are "free") on my (small) local network. It's an embedded Busybox Linux so I need a minimalist answer that hopefully doesn't require porting some library. I don't have libnet or libpcap. The arp cache seems to never contain anything but the MAC if the DHCP host.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Full source here.

Open /proc/net/arp, then read each line like this:

char line[500]; // Read with fgets().
char ip_address[500]; // Obviously more space than necessary, just illustrating here.
int hw_type;
int flags;
char mac_address[500];
char mask[500];
char device[500];

FILE *fp = xfopen("/proc/net/arp", "r");
fgets(line, sizeof(line), fp);    // Skip the first line (column headers).
while(fgets(line, sizeof(line), fp))
{
    // Read the data.
    sscanf(line, "%s 0x%x 0x%x %s %s %s\n",
          ip_address,
          &hw_type,
          &flags,
          mac_address,
          mask,
          device);

    // Do stuff with it.
}

fclose(fp);

This was taken straight from BusyBox's implementation of arp, in busybox-1_21_0/networking/arp.c directory of the BusyBox 1.21.0 tarball. Look at the arp_show() function in particular.

If you're scared of C:

The command arp -a should give you what you want, both MAC addresses and IP addresses.

To get all MAC addresses on a subnet, you can try

nmap -n -sP <subnet>
arp -a | grep -v incomplete
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!