ubuntu使用域名解析的示例:
1.获取域名解析的程序,将以下代码保存为“main.cpp”。
#include
#include
#include
#include
extern int h_errno;
int main(int argc, char **argv)
{
if (argc != 2) {
printf("Use example: %s www.google.com\n", *argv);
return -1;
}
char *name = argv[1];
struct hostent *hptr;
hptr = gethostbyname(name);
if (hptr == NULL) {
printf("gethostbyname error for host: %s: %s\n", name, hstrerror(h_errno));
return -1;
}
//输出主机的规范名
printf("\tofficial: %s\n", hptr->h_name);
//输出主机的别名
char **pptr;
char str[INET_ADDRSTRLEN];
for (pptr=hptr->h_aliases; *pptr!=NULL; pptr++) {
printf("\ttalias: %s\n", *pptr);
}
//输出ip地址
switch (hptr->h_addrtype) {
case AF_INET:
pptr = hptr->h_addr_list;
for (; *pptr!=NULL; pptr++) {
printf("\taddress: %s\n",
inet_ntop(hptr->h_addrtype, hptr->h_addr, str, sizeof(str)));
}
break;
default:
printf("unknown address type\n");
break;
}
return 0;
}
2.编译程序,命令:
gcc main.cpp //会生成a.out文件
3.重命名&&拷贝&&权限修改,命令:
sudo cp a.out /usr/bin/dp
sudo chmod a+x /usr/bin/dp
4..执行域名解析,例如:
dp www.baidu.com
5.返回结果如下:
official: www.a.shifen.com
talias: www.baidu.com
address: 180.97.33.108
address: 180.97.33.108