/* * @(#)Dns2000.java 1.0 2001/09/20 * * Copyright (c) 2001 Michel Gallant. * @author Michel Gallant */ import javax.naming.*; import javax.naming.directory.*; import java.util.*; import java.net.*; import java.io.*; /** * This utility class uses jndi and DNS service provider for DNS * reverse lookup. Applies to Win2000, and uses native "nslookup" * application to find default DNS server, if not provided as args[1]. * InetAddress methods are used for usual forward host-name ---> IP lookup. * Requires Java2 v1.3+ and JNDI and DNS service provider installed. * @author Michel Gallant * @version 1.00, 20/09/2001 */ public class Dns2000{ private static String dnsserver = "nsctor1.bellnexxia.net" ; //default dns server private static final String nslookup = "nslookup.exe localhost" ; private static StringTokenizer str ; public static void main(String[] args){ String addrstring = "" ; InetAddress adr = null ; InetAddress adrs[] ; // list of all IP addresses for given host name. int octet =0; BufferedReader dis = null; if(!System.getProperty("os.version").equals("5.0")) { usage(); } // ---------------- Use Win2000 nslookup.exe command to extract DNS server name ---------- if(args.length>1) { dnsserver = args[1] ; } else { // if no dns server given, find the default one using nslookup.exe try { Runtime rt = Runtime.getRuntime() ; Process proc = rt.exec(nslookup) ; dis = new BufferedReader(new InputStreamReader(proc.getInputStream())) ; str = new StringTokenizer(dis.readLine()); // first line of output contains DNS server name str.nextToken(); dnsserver = str.nextToken() ; proc.destroy() ; dis.close() ; // close } catch (Exception inte) { System.out.println("Couldn't get Domain Name Server name") ; System.exit(1) ; } } try { adr = InetAddress.getLocalHost() ; if(args.length>0) { // if command line parameter passed if(args[0].equals("?")) usage() ; adr = InetAddress.getByName(args[0]) ; str = new StringTokenizer(args[0], ".") ; // use dot as separator. try { while (str.hasMoreTokens()) octet = Integer.parseInt(str.nextToken()) ; // check to see if numeric IP address } catch(NumberFormatException nfe) { // a host NAME was provided, use usual Java methods since fqdn is assumed given. System.out.println("\nDNS Server: " + dnsserver) ; System.out.println("Host Name: " +adr.getHostName() ) ; adrs = InetAddress.getAllByName(args[0]) ; System.out.print("Address: ") ; for (int j = 0; j=0; i--) addrstring += (addr[i] & 0xFF) + "." ; // build reverse IP address Hashtable env = new Hashtable(); env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); env.put("java.naming.provider.url", "dns://" + dnsserver + "/."); DirContext ictx = new InitialDirContext(env); Attributes attrs1 = ictx.getAttributes(addrstring + "in-addr.arpa", new String[] {"PTR"}); Attribute attrib = attrs1.get("PTR") ; NamingEnumeration attrvals = attrib.getAll() ; System.out.println("\nDNS Server: " + dnsserver) ; System.out.print("Host Name: ") ; while(attrvals.hasMore()) System.out.print((String)attrvals.next() + " ") ; System.out.println("") ; System.out.println("Host Address: " +adr.getHostAddress() ) ; } catch(Exception e) { System.out.println(""); System.out.println(e.toString()); } } public static void usage() { System.out.println("\n-------------------------- Dns2000 ------------------------------------") ; System.out.println(" M. Gallant 09/20/2001 ") ; System.out.println("\nUsage: java Dns2000 [host] [dns server] ") ; System.out.println("DNS lookup utility for Win2000") ; System.out.println("Requires java2 v1.3+ and DNS service provider") ; System.out.println("If [dns server] not specified, uses Win2000 nslookup.exe to get dns server") ; System.out.println("If [host] not specified, returns information for current machine") ; System.out.println("-------------------------------------------------------------------------") ; System.exit(0) ; } }