DNS域名解析二方法

一:

你可以使用一些命令来做那些查找。比如,Linux使用host命令,但Windows使用nslookup。

Host命令

host命令接受带主机域名全称或IP地址的参数。要进行前向查找,使用如下命令行:

[root@bigboy tmp]# host www.ithov.com
www.ithov.com has address 65.115.71.34
[root@bigboy tmp]#

要进行反向查找

[root@bigboy tmp]# host 65.115.71.34
34.71.115.65.in-addr.arpa domain name pointer 65-115-71-34.myisp.net.
[root@bigboy tmp]#

如你所见,前向查找和反向查找结果不匹配。反向查找结果与ISP匹配。

nslookup命令

nslookup命令给Windows电脑提示类似的功能。要进行前向查找,使用:

C:\> nslookup www.ithov.com
Server:  192-168-1-200.my-site.com
Address:  192.168.1.200

Non-authoritative answer:
Name:    www.ithov.com
Address:  65.115.71.34

C:\>

要进行反向查找

C:\> nslookup 65.115.71.34
Server:  192-168-1-200.my-site.com
Address:  192.168.1.200

Name:    65-115-71-34.my-isp.com
Address:  65.115.71.34

C:\>

  1. #include <stdio.h>    
  2. #include <sys/types.h>    
  3. #include <sys/socket.h>    
  4. #include <netinet/in.h>    
  5. #include <arpa/inet.h>    
  6. #include <netdb.h>    
  7.    
  8. int main(int argc ,char **argv)    
  9. {    
  10.     struct sockaddr_in addr;    
  11.     struct hostent *host;    
  12.     char **alias;    
  13.     if(argc<2)    
  14.     {    
  15.         fprintf(stderr,"Usage:%s hostname|ip..\n\a",argv[0]);    
  16.         exit(1);    
  17.     }    
  18.     argv++;    
  19.     for(;*argv!=NULL;argv++)    
  20.     {    
  21.         /* 这里我们假设是IP*/       
  22.         if(inet_aton(*argv,&addr.sin_addr)!=0)    
  23.         {    
  24.             host=gethostbyaddr((char   *)&addr.sin_addr,4,AF_INET);     
  25.             printf("Address information of Ip %s\n",*argv);     
  26.         }     
  27.         else     
  28.         {    
  29.             /* 域名*/    
  30.             host=gethostbyname(*argv); printf("Address information of host %s\n",*argv);     
  31.         }    
  32.         if(host==NULL)    
  33.         {    
  34.             /* 找不到*/    
  35.             fprintf(stderr,"No address information of %s\n",*argv);    
  36.             continue;    
  37.         }    
  38.         printf("Official host name %s\n",host->h_name);    
  39.         printf("Name aliases:");    
  40.         for(alias=host->h_aliases;*alias!=NULL;alias++)    
  41.             printf("%s ,",*alias);    
  42.         printf("\nIp address:\n");    
  43.         for(alias=host->h_addr_list;*alias!=NULL;alias++)    
  44.             printf("%s \n ,",inet_ntoa(*(struct in_addr *)(*alias)));    
  45.     }    
  46.     
  47. return 0;   
  48.    
  49. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值