| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <unistd.h> |
| 6 |
#include <strings.h> |
| 7 |
#include <stdio.h> |
| 8 |
#include <netdb.h> |
| 9 |
|
| 10 |
#define SZ_LINE 1024 |
| 11 |
|
| 12 |
int main(int argc, char **argv) |
| 13 |
{ |
| 14 |
int i; |
| 15 |
char host[SZ_LINE]; |
| 16 |
struct hostent *hostent; |
| 17 |
|
| 18 |
if( argc > 1 ) |
| 19 |
strcpy(host, argv[1]); |
| 20 |
else{ |
| 21 |
fprintf(stderr, "calling gethostname() ...\n"); |
| 22 |
if( gethostname(host, SZ_LINE) == -1 ){ |
| 23 |
perror("gethostname"); |
| 24 |
exit(1); |
| 25 |
} |
| 26 |
else{ |
| 27 |
fprintf(stderr, "host name is %s\n", host); |
| 28 |
} |
| 29 |
} |
| 30 |
fprintf(stderr, "calling gethostbyname(host) ...\n"); |
| 31 |
if( !(hostent = gethostbyname(host)) ){ |
| 32 |
perror("gethostbyname"); |
| 33 |
exit(1); |
| 34 |
} |
| 35 |
else{ |
| 36 |
fprintf(stderr, "gethostbyname() succeeded\n"); |
| 37 |
} |
| 38 |
fprintf(stderr, "printing ip address(es) for this host ...\n"); |
| 39 |
if( hostent ){ |
| 40 |
for(i=0; hostent->h_addr_list[i]; i++){ |
| 41 |
fprintf(stderr, "%x\n", *(int *)hostent->h_addr_list[i]); |
| 42 |
} |
| 43 |
} |
| 44 |
else{ |
| 45 |
fprintf(stderr, "ERROR: can't look up: %s\n", host); |
| 46 |
} |
| 47 |
return(0); |
| 48 |
} |