从 DNS 服务器拉取 MX 记录 [英] Pulling MX record from DNS server

查看:44
本文介绍了从 DNS 服务器拉取 MX 记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,需要我对 MX 记录进行 DNS 查找.我不确定是否有人有从事此类工作的经验,但如果您有经验,我们将不胜感激.

I am writing an application that is requiring me to do a DNS lookup for an MX record. I'm not sure if anyone has had experience doing this kind of work but if you do, any help would be appreciated.

我想要的是一个将发送电子邮件警报的应用程序.问题是我需要让应用程序能够查找域的 MX 记录.

The thing that I'm going for is an application that will send an e-mail alert. The problem is I need to have the application be able to lookup the MX record for a domain.

推荐答案

最简单的方法就是使用常用的工具.

The simplest method is to simply use commonly available tools.

基本的dig"命令将通过此查询将记录返回给您:

The basic "dig" command will return the records to you via this query:

dig mx example.com

如果您只想要带有 mx 记录的行...

If you want just the lines with the mx records...

dig mx example.com | grep -v '^;' | grep example.com

dig 在大多数 linux/unix 机器上可用.

dig is available on most linux / unix boxes.

如果您使用的是 Windows,则可以使用 nslookup

If you're on windows you can use nslookup

nslookup -type=mx example.com

然后只需解析这些常用工具的输出即可.

Then just parse the output of these common tools.

由于您将C"作为标签,我猜您正在寻找使用原始套接字进行 MX 查找的源代码.我从 http://www.developerweb.net/forum/showthread.php 复制了这个?t=3550.可能是您要找的更多?

Since you put "C" as a tag, I guess you're looking for source code to do MX lookups using raw sockets. I copied this from http://www.developerweb.net/forum/showthread.php?t=3550. It may be more what you're looking for?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <resolv.h>

int main (int argc, char *argv[])
{
    u_char nsbuf[4096];
    char dispbuf[4096];
    ns_msg msg;
    ns_rr rr;
    int i, j, l;

    if (argc < 2) {
        printf ("Usage: %s <domain>[...]
", argv[0]);
        exit (1);
    }

    for (i = 1; i < argc; i++) {
        l = res_query (argv[i], ns_c_any, ns_t_mx, nsbuf, sizeof (nsbuf));
        if (l < 0) {
            perror (argv[i]);
        } else {
#ifdef USE_PQUERY
/* this will give lots of detailed info on the request and reply */
            res_pquery (&_res, nsbuf, l, stdout);
#else
/* just grab the MX answer info */
            ns_initparse (nsbuf, l, &msg);
            printf ("%s :
", argv[i]);
            l = ns_msg_count (msg, ns_s_an);
            for (j = 0; j < l; j++) {
                ns_parserr (&msg, ns_s_an, j, &rr);
                ns_sprintrr (&msg, &rr, NULL, NULL, dispbuf, sizeof (dispbuf));
                printf ("%s
", dispbuf);
            }
#endif
        }
    }

    exit (0);
}

这篇关于从 DNS 服务器拉取 MX 记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆