char url[]="www.BAIDU.COM"
char tmp_file_name[]="tmp.txt"
FILE *fp
sprintf(cmd,"nslookup %s >%s",url,tmp_file_name)
system(cmd)
if ( (fp=fopen(tmp_file_name,"r"))==NULL ){
printf("can not open %s\n",tmp_file_name)
exit(0)
}
// 然后按行读。跳过前两行。找后面的行。
// 如果某行第一个字符串是Addresses:
// 第二个字符串就是 IP
tmp.txt:
Server: aaaaaaaaaa
Address: nnn.nnn.nnn.nnn
Non-authoritative answer:
Name:www.a.shifen.COM
Addresses: 119.75.213.61, 119.75.216.30
Aliases: www.BAIDU.COM
#include<stdio.h> //printf#include<string.h> //memset
#include<stdlib.h> //for exit(0)
#include<sys/socket.h>
#include<errno.h> //For errno - the error number
#include<netdb.h> //hostent
#include<arpa/inet.h>
int hostname_to_ip(char * , char *)
int main(int argc , char *argv[])
{
if(argc <2)
{
printf("Please provide a hostname to resolve")
exit(1)
}
char *hostname = argv[1]
char ip[100]
hostname_to_ip(hostname , ip)
printf("%s resolved to %s" , hostname , ip)
printf("\n")
}
/*
Get ip from domain name
*/
int hostname_to_ip(char * hostname , char* ip)
{
struct hostent *he
struct in_addr **addr_list
int i
if ( (he = gethostbyname( hostname ) ) == NULL)
{
// get the host info
herror("gethostbyname")
return 1
}
addr_list = (struct in_addr **) he->h_addr_list
for(i = 0 addr_list[i] != NULL i++)
{
//Return the first one
strcpy(ip , inet_ntoa(*addr_list[i]) )
return 0
}
return 1
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)