Solved

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

Posted on 2002-07-09
5
1,639 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
[X]
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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

751 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