Sophie

Sophie

distrib > Mageia > 5 > x86_64 > media > core-release > by-pkgid > c7f285fde176f00924ccaa028758a807 > files > 6

dnsjava-2.1.3-1.mga5.noarch.rpm

<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 &lt; 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));            // &gt; 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 &lt; n.labels(); i++)
	System.out.println(n.getLabelString(i));
</code></pre>

</body>
</html>