Advertisement
AntonioVillanueva

Get IP ifconfig code

May 15th, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <arpa/inet.h>
  2. #include <ifaddrs.h>
  3. #include <string.h>
  4.  
  5. #include <string> //c++
  6. #include <iostream>
  7.  
  8. using namespace std;
  9. int main ()
  10. {
  11.     struct ifaddrs *ifap, *ifa;
  12.     std::string addr,mask;
  13.    
  14.     getifaddrs (&ifap);
  15.     for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
  16.         if (ifa->ifa_addr->sa_family==AF_INET && strcmp (ifa->ifa_name ,"lo")!=0) {
  17.             addr = inet_ntoa( ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr);        
  18.             mask = inet_ntoa( ((struct sockaddr_in *) ifa->ifa_netmask)->sin_addr);  
  19.            cout <<"Interface :"<<ifa->ifa_name<<" : " <<addr<<endl;
  20.            cout <<"mask  :"<<mask<<" : " <<endl;          
  21.             //freeifaddrs(ifap);      
  22.         }
  23.     }
  24.     freeifaddrs(ifap);
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement