#include <stdio.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <netinet/in.h> #include <net/if.h> #include <string.h> #include <arpa/inet.h> int main(int argc, char **argv) { int sockfd; struct ifreq ifr; char *ifname="eth0"; strncpy(ifr.ifr_name, ifname, IFNAMSIZ-1); sockfd = socket(AF_INET, SOCK_STREAM, 0); if(ioctl(sockfd, SIOCGIFADDR, &ifr) == -1) { printf("Error\n"); return; } struct sockaddr_in *myaddr; myaddr = (struct sockaddr_in*)&(ifr.ifr_addr); printf("IP: %s\n", inet_ntoa(myaddr->sin_addr)); }