如何确定c#中的dns更改? [英] How to determinde dns changes in c#?

查看:142
本文介绍了如何确定c#中的dns更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想监视dns地址更改。所以我需要跟踪dns的变化。我现在正在使用线程。我得到dns并保存它的文件,然后我比较他们每10秒,但我需要更具体的解决方案。例如,有没有事件呢?
这是代码:

I want to monitor the dns address changes. So i need to track dns changes. I am doing it with thread right now. I get dns and save it file and then i compare they every 10 sec but i need more specific solution. For exampe, is there any event for that? This is the code:

GetDns:

public List<string> GetDns()
    {
        List<string> dns = new List<string>();
        NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface networkInterface in networkInterfaces)
        {
            if (networkInterface.OperationalStatus == OperationalStatus.Up)
            {
                IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
                IPAddressCollection dnsAddresses = ipProperties.DnsAddresses;
                foreach (IPAddress dnsAdress in dnsAddresses)
                {
                        dns.Add(dnsAdress.ToString());
                }
            }
        }
        return dns;
    }

这是比较:

string[] xmlDns = xmlData.GetDatas("DNSs", "Dns");
        List<string> dns = getData.GetDns();
        for (int i = 0; i < xmlDns.Length; i++)
        {
                if ( xmlDns[i].Equals( dns[i]))
                {
                    this.Invoke(new MethodInvoker(delegate()
                    {
                        listBoxCheck.Items.Add(xmlDns[i] + " DNS was not changed.");
                    }));
                }
                else
                {
                    this.Invoke(new MethodInvoker(delegate()
                    {
                        listBoxCheck.Items.Add(xmlDns[i] + " DNS adress was changed as " + dns[i] );
                    }));
                }
        }


推荐答案

是基于请求。没有从DNS服务器到客户端的事件。您可以使用TTL(生存时间)元数据,它会告诉您DNS记录何时过期。

DNS is request based. There's no event from the DNS server to the client. You could potentially use the TTL (time to live) metadata, which tells you when the DNS record will expire.

这是一个公开API来查询给定的dns服务器的库 SimpleDNS 以及获取 TTL值的文档

Here's a library that exposes API to query a given dns server SimpleDNS and the documentation for getting the TTL value.

这篇关于如何确定c#中的dns更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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