~ubuntu-branches/ubuntu/wily/dnsjava/wily-proposed

« back to all changes in this revision

Viewing changes to examples.html

  • Committer: Bazaar Package Importer
  • Author(s): Thierry Carrez
  • Date: 2009-07-21 15:17:03 UTC
  • Revision ID: james.westby@ubuntu.com-20090721151703-6v0107p1s3h7gv1c
Tags: upstream-2.0.6
ImportĀ upstreamĀ versionĀ 2.0.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<html>
 
2
<head>
 
3
<title>dnsjava examples</title>
 
4
</head>
 
5
 
 
6
<body bgcolor="white">
 
7
 
 
8
<h1 align="center">dnsjava examples</h1>
 
9
 
 
10
All of these examples are code fragments.  Code using these fragments should
 
11
check exceptions when appropriate, and should:
 
12
 
 
13
<pre><code>import org.xbill.DNS.*;</code></pre>
 
14
 
 
15
<p><b>Get the IP address associated with a name:</b></p>
 
16
<pre><code>
 
17
InetAddress addr = Address.getByName("www.dnsjava.org");
 
18
</code></pre>
 
19
 
 
20
<br>
 
21
 
 
22
<p><b>Get the MX target and preference of a name:</b></p>
 
23
<pre><code>
 
24
Record [] records = new Lookup("dnsjava.org", Type.MX).run();
 
25
for (int i = 0; i &lt; records.length; i++) {
 
26
        MXRecord mx = (MXRecord) records[i];
 
27
        System.out.println("Host " + mx.getTarget() + " has preference ", mx.getPriority());
 
28
}
 
29
</code></pre>
 
30
 
 
31
<br>
 
32
 
 
33
<p><b>Query a remote name server for its version:</b></p>
 
34
<pre><code>
 
35
Lookup l = new Lookup("version.bind.", Type.TXT, DClass.CH);
 
36
l.setResolver(new SimpleResolver(args[0]));
 
37
l.run();
 
38
if (l.getResult() == Lookup.SUCCESSFUL)
 
39
        System.out.println(l.getAnswers()[0].rdataToString());
 
40
</code></pre>
 
41
 
 
42
<br>
 
43
 
 
44
<p><b>Transfer a zone from a server and print it:</b></p>
 
45
<pre><code>
 
46
ZoneTransferIn xfr = ZoneTransferIn.newAXFR(new Name("dnsjava.org"), "204.152.186.163", null);
 
47
List records = xfr.run();
 
48
for (Iterator it = records.iterator(); it.hasNext(); )
 
49
        System.out.println(it.next());
 
50
</code></pre>
 
51
 
 
52
<br>
 
53
 
 
54
<p><b>Use DNS dynamic update to set the address of a host to a value specified on the command line:</b></p>
 
55
<pre><code>
 
56
Name zone = Name.fromString("dyn.test.example.");
 
57
Name host = Name.fromString("host", zone);
 
58
Update update = new Update(zone);
 
59
update.replace(host, Type.A, 3600, args[0]);
 
60
 
 
61
Resolver res = new SimpleResolver("10.0.0.1");
 
62
res.setTSIGKey(new TSIG(host, base64.fromString("1234")));
 
63
res.setTCP(true);
 
64
 
 
65
Message response = res.send(update);
 
66
</code></pre>
 
67
 
 
68
<br>
 
69
 
 
70
<p><b>Manipulate domain names:</b></p>
 
71
<pre><code>
 
72
Name n = Name.fromString("www.dnsjava.org");
 
73
Name o = Name.fromString("dnsjava.org");
 
74
System.out.println(n.subdomain(o));            // True
 
75
 
 
76
System.out.println(n.compareTo(o));            // &gt; 0
 
77
 
 
78
Name rel = n.relativize(o);                    // the relative name 'www'
 
79
Name n2 = Name.concatenate(rel, o);
 
80
System.out.println(n2.equals(n));              // True
 
81
 
 
82
// www
 
83
// dnsjava
 
84
// org
 
85
for (int i = 0; i &lt; n.labels(); i++)
 
86
        System.out.println(n.getLabelString(i));
 
87
</code></pre>
 
88
 
 
89
</body>
 
90
</html>