ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to get the IP address of my own machine
for display. The closest I have been able to get is the
local '127.0.0.1' number. I want the 123.45.67.8 number.
I suspect part of the problem is the ability to support
multiple interfaces, in my case I want the IP address of
the 'eth0' interface. So, is there a function I can call, or a
file I can parse to obtaine this info ?
Use the gethostbyname function. If you don't know the hostname then use the gethostname function to obtain it first. Just remember it's when you get the address back from gethostbyname that it's in network byte order.
If you want that in a standard dotted format you can use the inet_ntoa function.
On which interface? There is an ioctl for it, I can't remember it on top of my head, but a quick grep through the kernel for SIOC* should point you to the right direction. The simplest way however is to use gethostbyname(localhost), this uses the resolver to get a list of ip addresses.
Here's a part of a test pgm I've been using to get the IP address.
So far, I've only been able to get the 127.0.0.1 address. The
gethostname function returns the correct name ("cs_linux"),
but I still can't get the eth0 interface address. I'll see what info
I can find on the IOCTL approach.
You can use SIOCGIFADDR. It returns you a struct ifreq; which has the name of the interface and the address of the interface. This however requires you have a socket bound to the correct interface.
You probably want to use SIOCGIFCONF which returns you a list of interfaces and its struct ifreqs.
I would provide source if I had access to a Linux box. Unfortunately my modem died at home and am still waiting for my DSL connection.
I also heard that getifaddrs() is easier to use, so you might also like to check that out.
I was looking for SIOCGIFADDR at LQ, and I found only this posting.
Although you deal with a different issue, I hope you could advise.
There is a problem. Whenever I try to start OpenOffice or StarOffice I get a splash banner for about 15 - 30 minutes, before the program actually starts (if it does at all). Shell returns:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.