#!/bin/sh
# the next line restarts using tclsh -*- tcl -*- \
exec tclsh "$0" "$@"
#
# nslook --
#
#	This example shows how one can convert host names to
#	IP addresses and back. It shows how to combine the 
#	netdb and the dns command.
#
# Copyright (c) 1995-1996 Technical University of Braunschweig.
# Copyright (c) 1996-1997 University of Twente.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# @(#) $Id: nslook,v 1.4 1998/11/30 12:09:33 schoenw Exp $

package require Tnm 3.0

namespace import Tnm::*

# Check the arguments. If it is an IP address, try to lookup the
# hostname, else try to lookup the IP address.

foreach arg $argv {
    set msg ""
    if {[regexp {^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$} $arg]} {
	set code [catch {netdb hosts name $arg} res]
	if $code {
	    set code [catch {dns name $arg} res]
	}
    } else {
	set code [catch {netdb hosts address $arg} res]
	if $code {
	    set code [catch {dns address $arg} res]
	}
    }
    if $code {
	set res ""
	set msg "<lookup failed>"
    }
    if {[regexp {^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$} $arg]} {
	puts [format "%-16s %-22s %s" $arg $res $msg]
    } else {
	puts [format "%-16s %-22s %s" $res $arg $msg]
    }
}
