-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDNSLookup.java
executable file
·65 lines (59 loc) · 2.37 KB
/
DNSLookup.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
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
{
private InitialDirContext iDirC;
public DNSLookup()
{
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
try {
iDirC = new InitialDirContext(env);
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void lookup (String host, String record)
{
InetAddress inetAddress;
try {
inetAddress = InetAddress.getByName(host);
// show the Internet Address as name/address
System.out.println(inetAddress.getHostName() + " " + inetAddress.getHostAddress());
// 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();
/* while (attributeEnumeration.hasMore())
{
System.out.println("" + attributeEnumeration.next());
}
attributeEnumeration.close();*/
Attribute mxRecord = attributes.get(record);
for (int i=0; i<mxRecord.size();i++)
System.out.println(mxRecord.get(i));
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
DNSLookup looker = new DNSLookup();
looker.lookup("truetech.com", "MX");
}
}