Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 75651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:32:58+00:00 2026-05-10T20:32:58+00:00

On a Linux box, the common interface names look like eth0, eth1, etc. I

  • 0

On a Linux box, the common interface names look like eth0, eth1, etc. I know how to find at least one IP address using gethostbyname or similar functions, but I don’t know any way to specify which named interface I want the IP address of. I could use ifconfig and parse the output, but shelling out for this information seems… inelegant.

Is there a way to, say, enumerate all the interfaces and their IP addresses (and maybe MAC addresses) into a collection? Or at least something along the lines of gethostbyinterface('eth0')?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:32:59+00:00Added an answer on May 10, 2026 at 8:32 pm
    // Originally from http://www.tlug.org.za/wiki/index.php/Obtaining_your_own_IP_address  #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <net/if.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> #include <stdio.h> #include <unistd.h>  /**  * getIPv4()  *  * This function takes a network identifier such as 'eth0' or 'eth0:0' and  * a pointer to a buffer of at least 16 bytes and then stores the IP of that  * device gets stored in that buffer.  *  * it return 0 on success or -1 on failure.  *  * Author:  Jaco Kroon <jaco@kroon.co.za>  */ int getIPv4(const char * dev, char * ipv4) {     struct ifreq ifc;     int res;     int sockfd = socket(AF_INET, SOCK_DGRAM, 0);      if(sockfd < 0)         return -1;     strcpy(ifc.ifr_name, dev);     res = ioctl(sockfd, SIOCGIFADDR, &ifc);     close(sockfd);     if(res < 0)         return -1;          strcpy(ipv4, inet_ntoa(((struct sockaddr_in*)&ifc.ifr_addr)->sin_addr));     return 0; }   int main() {     char ip[16];     if(getIPv4('eth0', ip) == 0)         printf('IPv4: %s\n', ip);     else         printf('No IP\n');     return 0;  } 

    Update: Moved dead link to a comment (for posterity) (thanks @obayhan), and added syntax highlighting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 88k
  • Answers 88k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This behaviour is because of the fact that RollingFileAppender (and… May 11, 2026 at 5:41 pm
  • Editorial Team
    Editorial Team added an answer First, create an index on HugeFileTable.file_last_access. Then try the following… May 11, 2026 at 5:41 pm
  • Editorial Team
    Editorial Team added an answer Just add an abstract copy method. You can use covariant… May 11, 2026 at 5:41 pm

Related Questions

I have run into an issue in which IE does not open up the
A common task in programs I've been working on lately is modifying a text
On a Linux box, I need to display the average CPU utilisation per hour
I have a directory on a linux box that I want to make publicly

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.