用C语言编写一个得到域名的IP的程序,得到的IP放入指定的变量中
我可以得出IP,但是不能把IP赋给变量,本人菜鸟一只,学C不到两个月,所以只能做到这了,代码给楼主
#includestdio.h
#includestdlib.h
void main()
{
char str[50],str1[40];
printf(“请输入域名:”);
gets(str1);
sprintf(str,”ping %s”,str1);
system(str);
}
满意就给分,不满意就不用了
用C语言编写一个域名提取的程序,如何选择抓包工具,域名的正则表达式怎么写?
用libpcap这个工具,很强大。
抓包部分在中文手册中有源代码。我就给你讲讲获取域名吧。
首先http包才有域名,并且是你三次握手后第一个包。
包到手后累加14+20+tcp-doff*4后边的信息中有host:*****的消息
14是MAC头 20是ip头 后边那个 是 tcp头 长度(由于tcp头长度不定,但是有标志位的。)
至于怎么提取你自己写吧
C语言中 如何实现域名转换为IP 高分
char cmd[100];
char url[]=””;
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:
Addresses: 119.75.213.61, 119.75.216.30
Aliases:
如何只用C语言,通过域名得到IP地址
#includestdio.h //printf
#includestring.h //memset
#includestdlib.h //for exit(0);
#includesys/socket.h
#includeerrno.h //For errno – the error number
#includenetdb.h //hostent
#includearpa/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;
}