Solved

Getting the local hostname and/or ip address on Linux

Posted on 2004-09-03
16
608 Views
Last Modified: 2012-05-05
Is there a simple way to get the hostname and/or ip address of the local machine in a C++ program on linux. Want to put it into a string variable.

One way I do it is that I run a System command(something like hostname>hostfile)from the program.....which writes the hostname into a file and then read from the file subsequently...but I kinda don't like it. There ought to be a better way.
0
Comment
Question by:sandeep_th
  • 9
  • 5
  • 2
16 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 11976714
You'd use

#include <netdb.h>

char acHostname [ 255];

gethostname ( acHostname, 255);
struct hostent * pHostEnt = gethostbyname ( acHostname);

The IP address is available via

short sIP = pHostEnt->h_addr;

and can be transformed into a string using 'inet_ntoa()':

char acNameAndAddress [512];

sprintf ( acNameAndAddress, "%s\t%s", acHostname, inet_ntoa ( pHostEnt->h_addr));
0
 
LVL 30

Expert Comment

by:Axter
ID: 11976815
>>One way I do it is that I run a System command(something like hostname>hostfile)from the
>>program.....which writes the hostname into a file and then read from the file subsequently...but I
>>kinda don't like it. There ought to be a better way.

jkr's method is the right approach.  But in the future, if you ever need to get the out put of a command, you don't have to put it into a file.
Instead you can use popen function.

Example:

FILE *fp = popen("hostname", "rt");
char acHostname [999];
fgets(acHostname , sizeof(acHostname ), fp);
0
 
LVL 30

Expert Comment

by:Axter
ID: 11976840
I forgot to mention, popen is included in stdio.h

#include <stdio.h>

see "man popen" for more info.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:sandeep_th
ID: 11977013
For jkr:

On compiling I get the following errors:

ta.cpp:21: error: invalid conversion from `char*' to `short int'
ta.cpp:27: error: conversion from `char*' to non-scalar type `in_addr'

line 21: short sIP = pHostEnt->h_addr;
line 27: sprintf ( acNameAndAddress, "%s\t%s", acHostname, inet_ntoa ( pHostEnt->h_addr));

For Axter:
I get a segmentation fault on the line:
fgets(acHostname , sizeof(acHostname ), fp);









0
 
LVL 86

Expert Comment

by:jkr
ID: 11977085
Sorry, that should be

in_addr iaIP = pHostEnt->h_addr;
sprintf ( acNameAndAddress, "%s\t%s", acHostname, inet_ntoa ( iaIP));
0
 

Author Comment

by:sandeep_th
ID: 11977160
Made no difference to the errors.

I did get the general drift though.

Here's one simple way I found for getting the hostname:

#include <unistd.h>

int gethostname(const char *name, int namelen);

If there is a similar one for the IP address....or to translate the ip address from the given hostname.....then this one is probably the simplest way.

0
 
LVL 86

Expert Comment

by:jkr
ID: 11977203
>>Here's one simple way I found for getting the hostname

Um, take a look at my 1st comment... line 3.

OK, so another try for that :o)

in_addr iaIP;
bcopy(iaIP, pHostEnt->h_addr, sizeof ( in_addr));
0
 

Author Comment

by:sandeep_th
ID: 11977338
right....sorry....I should have said "another simple way" :-)

The last line u sent also doesn't work :-(
0
 
LVL 86

Expert Comment

by:jkr
ID: 11977385
Ooops :o)

in_addr iaIP;
bcopy(&iaIP, pHostEnt->h_addr, sizeof ( in_addr));

It's getting late here...
0
 

Author Comment

by:sandeep_th
ID: 11977478
Hey...it works finally....sorta....'cause it returned a strange IP address:48.17.48.64
which is not even remotely close to the actual one!!!

The final code I used:

char acHostname [ 255];
gethostname ( acHostname, 255);
struct hostent * pHostEnt = gethostbyname ( acHostname);
char acNameAndAddress [512];
in_addr iaIP;
bcopy(&iaIP, pHostEnt->h_addr, sizeof ( in_addr));
sprintf ( acNameAndAddress, "%s\t%s", acHostname, inet_ntoa ( iaIP));
cout<<"HOSTNAME:"<<acHostname<<endl;
cout<<"IP ADDRESS:"<<acNameAndAddress<<endl;

0
 
LVL 86

Expert Comment

by:jkr
ID: 11977492
Have you verified that with ifconfig?
0
 

Author Comment

by:sandeep_th
ID: 11977506
Yep.....haven't a clue where it came from!! It's not as if even a part of it is right. It is completely different from the real ip address.
0
 

Author Comment

by:sandeep_th
ID: 11977563
.....both internal and external
0
 

Author Comment

by:sandeep_th
ID: 11979850
Ok...this was the erroneous line:

sprintf ( acNameAndAddress, "%s\t%s", acHostname, inet_ntoa ( iaIP));

should be replaced by:

sprintf ( acNameAndAddress, "%s\t%s", acHostname, inet_ntoa(*((struct in_addr *)pHostEnt->h_addr)));

cheers

0
 

Author Comment

by:sandeep_th
ID: 11980167
I'd sure like to know more about Axter's approach...with these lines of code:

FILE *fp = popen("hostname", "rt");
char acHostname [999];
fgets(acHostname , sizeof(acHostname ), fp);

And why it gave a segmentation fault on the following line :
fgets(acHostname , sizeof(acHostname ), fp);

Would be good to know
0
 

Author Comment

by:sandeep_th
ID: 11980209
Ok....got the error in Axter's code also, the "HOW" of it:

FILE *fp = popen("hostname", "rt");

ought to be replaced by:

FILE *fp = popen("hostname", "r");


Now could anyone shed some light on the "WHY" of it?


0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

828 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