글 수 48
2009.06.16 09:30:21 (*.55.127.122)
46751
도메인 이름을 가지고 IP를 구하는 방법입니다. gethostbyname()을 이용하여 도메인에 관련된 정보를 구했고, 정보 중에 h_addr_list의 값에서 inet_ntoa()을 이용하여 IP주소 문자열을 구했습니다.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
int main( int argc, char **argv)
{
struct hostent *host_entry;
int ndx;
if ( 2 > argc)
{
printf( "사용하는 방법: ]$ a.out {도메인}\n");
exit( 1);
}
host_entry = gethostbyname( argv[1]);
if ( !host_entry)
{
printf( "gethostbyname() 실행 실패\n");
exit( 1);
}
for ( ndx = 0; NULL != host_entry->h_addr_list[ndx]; ndx++)
printf( "%s\n", inet_ntoa( *(struct in_addr*)host_entry->h_addr_list[ndx]));
return 0;
}
소스를 컴파일하고 실행하면 아래와 같습니다.
]$ gcc domain-to-ip.c ]$ ./a.out 사용하는 방법: ]$ a.out {도메인} ]$ ./a.out www.falinux.com 211.239.155.97 ]$ ./a.out www.daum.net 211.115.77.214 211.115.115.211 211.115.115.212 222.231.51.40 222.231.51.77 222.231.51.78 211.32.117.30 211.115.77.211 ]$


