gpt4 book ai didi

Java DNSLookup 获取 DNS 属性

转载 作者:行者123 更新时间:2023-11-30 09:25:31 25 4
gpt4 key购买 nike

我对网络 DNS 记录很陌生,需要一个可以运行查找以获取域(A、MX、NS)的 3 个主要 DNS 记录的程序。

我一直在这里和那里寻找 Java 解决方案,我的 final类如下:然而,我总是会得到一个NameException并且找不到它的原因。

[编辑]:问题似乎出在我们的互联网上,因为使用其他 wifi,程序运行得很好。

非常感谢,

import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Hashtable;

public class DNSLookup
{
public static void main(String args[])
{
String host = "google.com";
try
{
InetAddress inetAddress = InetAddress.getByName(host);
// show the Internet Address as name/address
System.out.println(inetAddress.getHostName() + " " + inetAddress.getHostAddress());

Hashtable<String, String> env = new Hashtable<String, String>();
//env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
//env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.dns.DnsContextFactory");
//env.put(Context.PROVIDER_URL, "dns://google.com");

// get the default initial Directory Context
InitialDirContext iDirC = new InitialDirContext(env);
// get the DNS records for inetAddress
Attributes attributes = iDirC.getAttributes("dns:/"+inetAddress.getHostName());
// get an enumeration of the attributes and print them out
NamingEnumeration<?> attributeEnumeration = attributes.getAll();
System.out.println("");
while (attributeEnumeration.hasMore())
{
System.out.println("" + attributeEnumeration.next());
}
attributeEnumeration.close();
}
catch (UnknownHostException exception)
{
System.err.println("ERROR: Cannot access '" + host + "'");
}
catch (NamingException exception)
{
System.err.println("ERROR: No DNS record for '" + host + "'");
exception.printStackTrace();
}
}
}

输出:

google.com 74.125.128.113
ERROR: No DNS record for 'google.com'
javax.naming.CommunicationException: DNS error [Root exception is java.net.SocketTimeoutException: Receive timed out]; remaining name 'google.com'
at com.sun.jndi.dns.DnsClient.query(Unknown Source)
at com.sun.jndi.dns.Resolver.query(Unknown Source)
at com.sun.jndi.dns.DnsContext.c_getAttributes(Unknown Source)
at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(Unknown Source)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(Unknown Source)
at com.sun.jndi.toolkit.url.GenericURLDirContext.getAttributes(Unknown Source)
at javax.naming.directory.InitialDirContext.getAttributes(Unknown Source)
at javax.naming.directory.InitialDirContext.getAttributes(Unknown Source)
at gimasys.dnsCrawler.DNSLookup.main(DNSLookup.java:35)
Caused by: java.net.SocketTimeoutException: Receive timed out
at java.net.DualStackPlainDatagramSocketImpl.socketReceiveOrPeekData(Native Method)
at java.net.DualStackPlainDatagramSocketImpl.receive0(Unknown Source)
at java.net.AbstractPlainDatagramSocketImpl.receive(Unknown Source)
at java.net.DatagramSocket.receive(Unknown Source)
at com.sun.jndi.dns.DnsClient.doUdpQuery(Unknown Source)
... 9 more

最佳答案

我已经尝试使用提供的类 (DNSLookup.java),它对我有用。

我的理解是它给出超时异常,意味着它无法连接到服务器。

Check your internet connection ?

关于Java DNSLookup 获取 DNS 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15239220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com