/* Compile with: gcc -lnsl -o getip getip.c * * This utility is written because of the lack of utility returning IP * address from hostname regardless of name service used, hosts table, * NIS, or DNS. arp comes close, but it does not return all IPs a hostname * is associated. * * March 1999, fengyue@bluerose.windmoon.nu pointed out that stdio.h needs * to be included for NULL, and host_ent needs to be tested for NULL before * using. * * Feburary 1998, put on network. * */ #include #include #include #include #include #include int main(int argc, char **argv) { const char *host_name; struct hostent *host_ent; char **host_list; struct in_addr *host_number; char *host_ip; int i; if (argc == 1) { printf("\n"); printf("getip hostname1 hostname2 ...\n"); printf("\n"); printf("Example: \n"); printf("# getip wind moon windmoon sun\n"); printf("12.5.8.11\n"); printf("12.5.8.12 12.5.8.13\n"); printf("unknown_host\n"); printf("12.5.8.14\n"); printf("\n"); printf("Send comments to mwang@mindspring.com (Michael Wang).\n"); printf("\n"); exit(100); } while ( --argc > 0 ) { host_name = *++argv; host_ent = gethostbyname(host_name); if ( host_ent != NULL ) { host_list = host_ent->h_addr_list; i=0; while ( (host_number = (struct in_addr *) *host_list++ ) != NULL ) { host_ip = inet_ntoa(*host_number); if ( i != 0 ) { printf(" "); } printf("%s", host_ip); i++; } printf("\n"); } else { printf("unknown_host\n"); } } }