java 获取dns,Java DNSLookup获取DNS属性

I'm quite new to the network DNS records and need a program that can run lookup to get 3 main DNS records of a domain (A,MX,NS).

I have been looking for a java solution here an there and my final class is as below:

However, I will always get a NameException and can't find the reason for it.

[EDIT]: The problem seem to be with our internet, because using other wifi, the program run just fine.

Many thanks,

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 env = new Hashtable();

//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();

}

}

}

Output:

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

解决方案

I have tried with provided class (DNSLookup.java), it's working for me.

My underestanding is it's giving timeOut exception, means its not able to connect to server.

Check your internet connection ?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值