Linux - How to get my IP in C/C++

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

rockballad

Thread Starter
Joined
May 4, 2007
Messages
78
Hi,

I'm writing a small program to get my computer IP on LAN (e.g. 192.168.x.x, it's not 127.0.x.x). In Windows, it's simple, but I can't do it in Linux (code is quite similar). If you know about it, could you show me please?

Thanks in advance.
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
I can't do it in C or C++ but a shell script would be super easy. I can do just about anything in Linux with a shell script.
Code:
ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' |
cut -d: -f2 | awk '{ print $1}'
 

rockballad

Thread Starter
Joined
May 4, 2007
Messages
78
Thank you! It should work well! I just set some first steps on Linux, so your help is really appreciated! I'll search on how to grab the output of a commandline or something like that. But, I think indeed that must be a way to do in C/C++.

Cheers,
 
Joined
Sep 12, 2003
Messages
20,583
Squashman said:
I can't do it in C or C++ but a shell script would be super easy. I can do just about anything in Linux with a shell script.
Code:
ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' |
cut -d: -f2 | awk '{ print $1}'
Hi Squashman,

Your example doesn't work correctly as it needs to consider the context of whether a user's connection is dialup or not:

if a user is dialup then the following should work:
ifconfig ppp0 | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'

otherwise,

your scripting produces the following on a dialup system with an ethernet card (unplugged):
eth0:avah: <n.n.n.n>
ppp0: <the correct ip address>

-- Tom
 
Joined
Sep 12, 2003
Messages
20,583
rockballad said:
Thank you! It should work well! I just set some first steps on Linux, so your help is really appreciated! I'll search on how to grab the output of a commandline or something like that. But, I think indeed that must be a way to do in C/C++.

Cheers,
Hi rockballad,

One way to feed a value in your computing environment is to assign the found ip address to an environment variable, and then read the environment variable into the c/c++ program.

-- Tom
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
lotuseclat79 said:
Hi Squashman,

Your example doesn't work correctly as it needs to consider the context of whether a user's connection is dialup or not:

if a user is dialup then the following should work:
ifconfig ppp0 | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'

otherwise,

your scripting produces the following on a dialup system with an ethernet card (unplugged):
eth0:avah: <n.n.n.n>
ppp0: <the correct ip address>

-- Tom
Yes, but he asked for is LAN IP.

rockballad said:
Hi,

I'm writing a small program to get my computer IP on LAN (e.g. 192.168.x.x, it's not 127.0.x.x). In Windows, it's simple, but I can't do it in Linux (code is quite similar). If you know about it, could you show me please?

Thanks in advance.
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
lotuseclat79 said:
Ooops, missed that!

-- Tom
Don't Sweat it. You are still the Linux Guru around here since Code Jockey hasn't been around much.
 

rockballad

Thread Starter
Joined
May 4, 2007
Messages
78
Thank you all.

windows user said:
Call gethostname() to get your hostname and then call gethostbyname() to get your IP address. See http://beej.us/guide/bgnet/output/html/multipage/gethostbynameman.html
Yes, it's expected to work. But it's not :( The func gethostbyname("localhost") returns "127.0.0.1" only. And, gethostbyname("mycomputername") returns "127.0.1.1".

From the article, I though that why I don't try to get the *real* host from the IP (that I expect to get).

The command "ifconfig" in Terminal let me know that the IP on LAN is "192.168.1.5". So by using
Code:
inet_aton("192.168.1.5", &addr);
h = gethostbyaddr(&addr, sizeof addr, AF_INET);
I know the *real* host name is "mycomputername.local"! That's it. The difference is the tail ".local". Could any one tell me more about this tail and others? Maybe this's the difference between Linux and Windows, isn't it?

Thanks again! (y)
 
Joined
Sep 12, 2003
Messages
20,583
Hi flowerbed,

Welcome to TSG!

You need to read the forum rules.

Then use the New Thread button at the top of the Forum webpage to open up your own new thread.

-- Tom
 
Joined
Sep 3, 2007
Messages
60
Post your code. Here's what I did and it worked. I copied and pasted from Beej's tutorial:
Code:
#include <sys/unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>

int main()
{
 char hostname[128];
 int i;
 struct hostent *he;
 struct in_addr **addr_list;
 struct in_addr addr;

 gethostname(hostname, sizeof hostname);
 printf("My hostname: %s\n", hostname);

 he = gethostbyname(hostname);

 if (he == NULL) { // do some error checking
   herror("gethostbyname"); // herror(), NOT perror()
   return 1;
 }

 // print information about this host:
 printf("Official name is: %s\n", he->h_name);
 printf("IP address: %s\n", inet_ntoa(*(struct in_addr*)he->h_addr));
 printf("All addresses: ");
 addr_list = (struct in_addr **)he->h_addr_list;

 for(i = 0; addr_list[i] != NULL; i++) {
   printf("%s ", inet_ntoa(*addr_list[i]));
 }

 printf("\n");

 return 0;
}
Output:

My hostname: kara
Official name is: kara
IP address: <something that isn't 127.0.0.1>
All addresses: <something that isn't 127.0.0.1>
 

tomdkat

Retired Trusted Advisor
Joined
May 6, 2006
Messages
7,148
windows user said:
Call gethostname() to get your hostname and then call gethostbyname() to get your IP address.
Yep, that's exactly what I was going to suggest. Plus, this should work across platforms as well.

Peace...
 

tomdkat

Retired Trusted Advisor
Joined
May 6, 2006
Messages
7,148
rockballad said:
I know the *real* host name is "mycomputername.local"! That's it. The difference is the tail ".local". Could any one tell me more about this tail and others? Maybe this's the difference between Linux and Windows, isn't it?
It relates to how your local hosts file is setup along with the DNS configuration. In your local hosts file, you will have an entry for "localhost" and possibly for your actual local host name. This is why gethostname() is called first, so you can get your actual host name. Then you call gethostbyname() to get the IP address.

So, I imagine on your system, gethostname() returns "mycomputername.local". You then pass this to gethostbyname() to get your IP address, as described in the tutorial linked to above.

Attached is a screenshot of the hosts file for my Gentoo Linux system running in VirtualBox on Windows XP, as an example.

Peace...
 

Attachments

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top