Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How can we get the ip address like ifconfig using C program?

Posted on 2002-07-09
5
1,632 Views
Last Modified: 2008-02-01
How can we get the ip address like ifconfig using C program?
The question is not the same as what I asked.
The Ip I got was not same as the ifconfig.
I beleive that using ifconfig can get the true ip address
and can ping the IP.
So gethostent(),gethostbyname(), maybe retrieve the ip
address from /etc/hosts,....
Can you help me to write a code like ifconfig
which is not use gethostent with reading /etc/hosts,
(Because /etc/hosts is wrong in my computer.)
 




Because I use rc.inet1 to set the IP of mycomputer,
These files /etc/hosts , /etc/hostname  was not corrected.
That is to say,for example ,
the ip /etc/hosts is 192.168.0.1,
but in the rc.inet1 192.168.0.3,
when using ifconfig , I find the eth0 is 192.168.0.3.
I used Slakware 7.0 distribution.
0
Comment
Question by:mathematics
5 Comments
 

Expert Comment

by:dhanaspace1
ID: 7146535

some bad way & quick way
system ("ifconfig > /tmp/temp_file")
open the file
then strstr (buffer,"inet addr)
read the next token.
0
 
LVL 5

Accepted Solution

by:
bryanh earned 50 total points
ID: 7151625
Create a socket in the Ineternet domain and issue a SIOCGIFADDR ioctl to it

  #include <if.h>
  #include <in.h>
  #include <sockios.h>

  int sockfd;
  struct ifreq ifreq;
  struct sockaddr_in sockaddr;
  struct in_addr ipaddr;
  int rc;

  strcpy(ifreq.ifr_ifrn.ifrn_name, "eth0");

  sockfd=socket(PF_INET, IPPROTO_IP)
  rc = ioctl(sockfd, SIOCGIFADDR, &ifreq)

  sockaddr = (struct sockaddr_in) ifreq.ifr_ifru.ifru_addr;

  ipaddr = sockaddr.sin_addr;
0
 

Assisted Solution

by:pianopan
pianopan earned 50 total points
ID: 7213250
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <stdio.h>

int
main(int argc,char *argv[])
{
  char * host,**names,**addrs;
  struct hostent *hostinfo;

  if(argc == 1)
    {
      char myname[256];
      gethostname(myname,255);
      host=myname;
    } else if(argc != 2)
        {
          printf("Usage:%s <hostname>\n",argv[0]);
          exit(0);
        }else
  host=argv[1];

  hostinfo=gethostbyname(host);
 
  if(!hostinfo)
    {
      printf("can't get info of host: %s\n",host);
      exit(1);
    }
  printf("-------%s:infomation-----\n",host);
  printf("Offical name:%s\n",hostinfo->h_name);
  printf("Alias:\n");
  names=hostinfo->h_aliases;
  while(*names){
    printf("%s\n",*names);
    names++;
    }
  if(hostinfo->h_addrtype != AF_INET)
    {
        printf("not a IP host\n");
        exit(1);
    }
  printf("Address:\n");
  addrs=hostinfo->h_addr_list;
  while(*addrs)
    {
        printf("%s\n",inet_ntoa(*(struct in_addr *)*addrs));
        addrs++;
    }
  printf("-------info end-----\n----http://www.calihotel.net----");
  exit(0);
}
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9925554
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: bryanh {http:#7151625} & pianopan {http:#7213250}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Where is my core dump file in Ubuntu? 12 453
OS & DB Recommendation 4 48
Shell script for mvn build 2 76
What is the timeframe of this cron job? 2 143
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question