版权声明:本文为博主原创文章,可以随意引用或转载,但未经博主允许不得用于任何商业用途。 https://blog.csdn.net/ustccw/article/details/80842852
1.目的
ubuntu 上,想快速的进行域名解析,可按照下面方式进行。
2.获取域名解析的程序
将下面代码保存为 main.cpp。
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
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;
}
3.编译程序
gcc main.cpp
将会看到生成的 a.out
文件
4.重命名 && 拷贝 && 权限修改
sudo cp a.out /usr/bin/dp
sudo chmod a+x /usr/bin/dp
5.执行域名解析
dp www.baidu.com
返回
official: www.a.shifen.com
talias: www.baidu.com
address: 180.97.33.108
address: 180.97.33.108