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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
<html>
<head>
<title>dnsjava examples</title>
</head>
<body bgcolor="white">
<h1 align="center">dnsjava examples</h1>
All of these examples are code fragments. Code using these fragments should
check exceptions when appropriate, and should:
<pre><code>import org.xbill.DNS.*;</code></pre>
<p><b>Get the IP address associated with a name:</b></p>
<pre><code>
InetAddress addr = Address.getByName("www.dnsjava.org");
</code></pre>
<br>
<p><b>Get the MX target and preference of a name:</b></p>
<pre><code>
Record [] records = new Lookup("gmail.com", Type.MX).run();
for (int i = 0; i < records.length; i++) {
MXRecord mx = (MXRecord) records[i];
System.out.println("Host " + mx.getTarget() + " has preference " + mx.getPriority());
}
</code></pre>
<br>
<p><b>Query a remote name server for its version:</b></p>
<pre><code>
Lookup l = new Lookup("version.bind.", Type.TXT, DClass.CH);
l.setResolver(new SimpleResolver(args[0]));
l.run();
if (l.getResult() == Lookup.SUCCESSFUL)
System.out.println(l.getAnswers()[0].rdataToString());
</code></pre>
<br>
<p><b>Transfer a zone from a server and print it:</b></p>
<pre><code>
ZoneTransferIn xfr = ZoneTransferIn.newAXFR(new Name("."), "192.5.5.241", null);
List records = xfr.run();
for (Iterator it = records.iterator(); it.hasNext(); )
System.out.println(it.next());
</code></pre>
<br>
<p><b>Use DNS dynamic update to set the address of a host to a value specified on the command line:</b></p>
<pre><code>
Name zone = Name.fromString("dyn.test.example.");
Name host = Name.fromString("host", zone);
Update update = new Update(zone);
update.replace(host, Type.A, 3600, args[0]);
Resolver res = new SimpleResolver("10.0.0.1");
res.setTSIGKey(new TSIG(host, base64.fromString("1234")));
res.setTCP(true);
Message response = res.send(update);
</code></pre>
<br>
<p><b>Manipulate domain names:</b></p>
<pre><code>
Name n = Name.fromString("www.dnsjava.org");
Name o = Name.fromString("dnsjava.org");
System.out.println(n.subdomain(o)); // True
System.out.println(n.compareTo(o)); // > 0
Name rel = n.relativize(o); // the relative name 'www'
Name n2 = Name.concatenate(rel, o);
System.out.println(n2.equals(n)); // True
// www
// dnsjava
// org
for (int i = 0; i < n.labels(); i++)
System.out.println(n.getLabelString(i));
</code></pre>
</body>
</html>
|