Solved

Getting the local hostname and/or ip address on Linux

Posted on 2004-09-03
16
602 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
Comment Utility
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
Comment Utility
>>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
Comment Utility
I forgot to mention, popen is included in stdio.h

#include <stdio.h>

see "man popen" for more info.
0
 

Author Comment

by:sandeep_th
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
right....sorry....I should have said "another simple way" :-)

The last line u sent also doesn't work :-(
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 86

Expert Comment

by:jkr
Comment Utility
Ooops :o)

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

It's getting late here...
0
 

Author Comment

by:sandeep_th
Comment Utility
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
Comment Utility
Have you verified that with ifconfig?
0
 

Author Comment

by:sandeep_th
Comment Utility
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
Comment Utility
.....both internal and external
0
 

Author Comment

by:sandeep_th
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now