DNSBrowse.java

back to DNS queries in Java

01 //
02 // DNSBrowse.java
03 //
04 // Simple test-bed for my DNSQuery class.
05 //
06 // Copyright 1997  W. Scott Means (http://smeans.com)
07 //
08 // Licensed under the Apache License, Version 2.0 (the "License");
09 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 //     http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 //  See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20 
21 import java.io.*;
22 import java.net.*;
23 
24 public class DNSBrowse 
25 {
26   public static void TestDNS(String strFQDN) {
27     DNSQuery dnsQ = new DNSQuery();
28 
29     dnsQ.SetQuery(dnsQ.TYPE_ANY, dnsQ.CLASS_INTERNET, strFQDN);
30 
31     if (DNSService.PerformDNSQuery(dnsQ)) {
32       for (int i = 0; i < dnsQ.m_arrAns.length; i++) {
33         dnsQ.m_arrAns[i].dumpRecord(System.out);
34         System.out.println("");
35       }
36     }
37   }
38 
39   public static void main(String args[]) {
40     if (args.length < 1) {
41       System.err.println("usage: DNSBrowse ns1.somehost.com [ns2.somehost.com] < domainlist.txt");
42       return;
43     }
44     
45     InetAddress iaPrimary;
46     InetAddress iaSecondary;
47     
48     try {
49       iaPrimary = InetAddress.getByName(args[0]);
50       
51       iaSecondary = args.length > ? InetAddress.getByName(args[1]) : iaPrimary;
52     catch (UnknownHostException uhe) {
53       System.err.println("exception: unable to resolve host: " + uhe.getMessage());
54 
55       return;
56     }
57 
58     System.out.println("Primary: " + iaPrimary.toString());
59     System.out.println("Secondary: " + iaSecondary.toString());
60 
61     int iChar;
62     String str;
63     DataInputStream dis = new DataInputStream(System.in);
64 
65     DNSService.SetDNSAddress(iaPrimary, iaSecondary);
66 
67     try {
68       while ((str = dis.readLine()) != null && str.length() 0) {
69         TestDNS(str);
70       }
71     catch (IOException ioe) {
72       System.out.println("exception: " + ioe.getMessage());
73     }
74   }
75 }
Java2html