java 设置dns_java指定dns

/**

* 获取DNS服务器信息

*

* @param domain  要获取DNS信息的域名

* @param provider      DNS服务器

* @param types   信息类型 "A"(IP信息),"MX"

* @param timeout 请求超时

* @param retryCount    重试次数

*

* @return 所有信息组成的数组

*

* @throws NamingException

*

*/

@SuppressWarnings("rawtypes" )

public static ArrayList getDNSRecs(String domain, String provider,

String [] types, int timeout, int retryCount) throws NamingException {

ArrayList results = new ArrayList(15);

Hashtable env = new Hashtable();

env.put( "java.naming.factory.initial" ,

"com.sun.jndi.dns.DnsContextFactory" );

//设置域名服务器

env.put(Context. PROVIDER_URL, "dns://" + provider);

// 连接时间

env.put( "com.sun.jndi.dns.timeout.initial" , String.valueOf(timeout));

// 连接次数

env.put( "com.sun.jndi.dns.timeout.retries" , String.valueOf(retryCount));

DirContext ictx = new InitialDirContext(env);

Attributes attrs = ictx.getAttributes(domain, types);

for (Enumeration e = attrs.getAll(); e.hasMoreElements();) {

Attribute a = (Attribute) e.nextElement();

int size = a.size();

for (int i = 0; i 

results.add((String) a.get(i));

}

}

return results;

}

/**

* 获取域名所有IP

* @param domain  域名

* @param dnsServers    DNS服务器列表

* @param timeout 请求超时

* @param retryCount    重试次数

* @return

*/

public static Set getAllIP(String domain, String[] dnsServers,

int timeout, int retryCount) {

Set ips = new HashSet();

for(String dnsServer: dnsServers) {

List ipList;

try {

ipList = getDNSRecs(domain, dnsServer, new String[]{"A"},

timeout, retryCount);

} catch (NamingException e) {

continue;

}

ips.addAll(ipList);

}

return ips;

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值