TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
使用 JNDI 进行DNS, 邮件服务器, 主机信息查找
程序如下:
import java.util.Hashtable;
import java.util.Enumeration;
import javax.naming.*;
import javax.naming.directory.*;
public class JNDISearchDNS {
public static void main(String args[]) {
try {
//Hashtable for environmental information
Hashtable env = new Hashtable();
//Specify which class to use for our JNDI provider
env.put("java.naming.factory.initial","com.sun.jndi.dns.DnsContextFactory");
env.put("java.naming.provider.url", "dns://129.120.210.252/");
String dns_attributes[] = {"MX","A","HINFO"};
//Get a reference to a directory context
DirContext ctx = new InitialDirContext(env);
Attributes attrs1 = ctx.getAttributes("www.163.com",dns_attributes);
if (attrs1 == null) {
System.out.println("host has none of the specified attributes ");
} else {
for (int z = 0; z < dns_attributes.length; z++) {
Attribute attr = attrs1.get(dns_attributes[z]);
if (attr != null) {
System.out.print(dns_attributes[z]+": ");
for (Enumeration vals = attr.getAll();vals.hasMoreElements();) {
System.out.println(vals.nextElement());
}
}
System.out.println(" ");
}
}
} catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
改程序转自书籍<<J2EE 编程指南 1.3 版>>.
运行结果:
C:java>java JNDISearchDNS
A: 202.108.36.196
202.106.168.103
202.106.168.104
202.106.168.109
202.106.168.121
202.108.36.153
202.108.36.155
202.108.36.156
202.108.36.167
202.108.36.172
C:java> |
|