Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the IP address of a (Linux) machine?

This Question is almost the same as the previously asked How can I get the IP Address of a local computer? -Question. However I need to find the IP address(es) of a Linux Machine.

So: How do I - programmatically in C++ - detect the IP addresses of the linux server my application is running on. The servers will have at least two IP addresses and I need a specific one (the one in a given network (the public one)).

I'm sure there is a simple function to do that - but where?


To make things a bit clearer:

  • The server will obviously have the "localhost": 127.0.0.1
  • The server will have an internal (management) IP address: 172.16.x.x
  • The server will have an external (public) IP address: 80.190.x.x

I need to find the external IP address to bind my application to it. Obviously I can also bind to INADDR_ANY (and actually that's what I do at the moment). I would prefer to detect the public address, though.

like image 960
BlaM Avatar asked Oct 17 '08 15:10

BlaM


People also ask

What is my IP from command line?

First, click on your Start Menu and type cmd in the search box and press enter. A black and white window will open where you will type ipconfig /all and press enter. There is a space between the command ipconfig and the switch of /all. Your ip address will be the IPv4 address.


2 Answers

I found the ioctl solution problematic on os x (which is POSIX compliant so should be similiar to linux). However getifaddress() will let you do the same thing easily, it works fine for me on os x 10.5 and should be the same below.

I've done a quick example below which will print all of the machine's IPv4 address, (you should also check the getifaddrs was successful ie returns 0).

I've updated it show IPv6 addresses too.

#include <stdio.h>       #include <sys/types.h> #include <ifaddrs.h> #include <netinet/in.h>  #include <string.h>  #include <arpa/inet.h>  int main (int argc, const char * argv[]) {     struct ifaddrs * ifAddrStruct=NULL;     struct ifaddrs * ifa=NULL;     void * tmpAddrPtr=NULL;      getifaddrs(&ifAddrStruct);      for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {         if (!ifa->ifa_addr) {             continue;         }         if (ifa->ifa_addr->sa_family == AF_INET) { // check it is IP4             // is a valid IP4 Address             tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;             char addressBuffer[INET_ADDRSTRLEN];             inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);             printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);          } else if (ifa->ifa_addr->sa_family == AF_INET6) { // check it is IP6             // is a valid IP6 Address             tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;             char addressBuffer[INET6_ADDRSTRLEN];             inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);             printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);          }      }     if (ifAddrStruct!=NULL) freeifaddrs(ifAddrStruct);     return 0; } 
like image 67
Twelve47 Avatar answered Sep 23 '22 10:09

Twelve47


  1. Create a socket.
  2. Perform ioctl(<socketfd>, SIOCGIFCONF, (struct ifconf)&buffer);

Read /usr/include/linux/if.h for information on the ifconf and ifreq structures. This should give you the IP address of each interface on the system. Also read /usr/include/linux/sockios.h for additional ioctls.

like image 27
Steve Baker Avatar answered Sep 19 '22 10:09

Steve Baker