home *** CD-ROM | disk | FTP | other *** search
- Path: wupost!zaphod.mps.ohio-state.edu!rpi!dali.cs.montana.edu!milton!nntp.uoregon.edu!cs.uoregon.edu!mystix.cs.uoregon.edu!bart
- From: bart@cs.uoregon.edu (Bart Massey)
- Newsgroups: alt.sources
- Subject: DPOTW1: ipaddrname
- Keywords: IP gethostbyname gethostbyaddr name address lookup
- Message-ID: <1991Aug31.020123.26380@cs.uoregon.edu>
- Date: 31 Aug 91 02:01:23 GMT
- Sender: bart@cs.uoregon.edu (Bart Massey)
- Followup-To: alt.sources.d
- Organization: Department of Computer Science, University of Oregon
- Lines: 137
- Originator: bart@mystix.cs.uoregon.edu
-
- Dumb Package Of The Month #1: ipaddrname
-
- Here's a dumb little name/address lookup program I whipped up a
- while back which I find myself using an inordinate amount.
- Basically, it just calls gethostbyname(3) or gethostbyaddr(3)
- and prints the result -- useful for debugging DNS, NIS, and
- /etc/hosts problems. Have fun!
-
- Bart Massey
- bart@cs.uoregon.edu
-
- #! /bin/sh
- # This is a "shar" archive. To extract, clip off any leading or
- # trailing garbage and execute using the Bourne Shell.
- echo "creating Makefile"
- sed 's/^X//' >Makefile <<'X'
- BIN = /local/bin
- MAN = /local/man/man1
- CFLAGS = -O
-
- all: ipaddr ipname
-
- ipaddr: ipaddrname.c
- $(CC) $(CFLAGS) -s -o ipaddr ipaddrname.c
-
- ipname: ipaddr
- -rm -f ipname
- ln -s ipaddr ipname
-
- install:
- -rm -f $(BIN)/ipaddr $(BIN)/ipname
- cp ipaddr ipname $(BIN)
- cp ipaddr.man $(MAN)/ipaddr.1
-
- clean:
- -rm -f *.o ipaddr ipname
- X
- echo "creating ipaddr.man"
- sed 's/^X//' >ipaddr.man <<'X'
- ..TH IPADDR 1
- ..SH NAME
- ipaddr, ipname \- quick hostname and IP address lookup
- ..SH SYNOPSIS
- ..B ipaddr
- ..I name
- ..br
- ..B ipname
- ..I address
- ..SH DESCRIPTION
- ..B ipaddr
- and
- ..B ipname
- call
- ..B "gethostbyname(3)"
- and
- ..B "gethostbyaddr(3),"
- respectively, with their argument,
- and report the results in a human-readable format.
- ..SH SEE ALSO
- gethostbyname(3), gethostbyaddr(3)
- ..SH DIAGNOSTICS
- Pretty much directly as returned by the library calls.
- ..SH BUGS
- Probably zillions.
- X
- echo "creating ipaddrname.c"
- sed 's/^X//' >ipaddrname.c <<'X'
- /*
- * ipaddrname -- host name/address lookup
- * Bart 9/91
- */
-
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #ifndef hpux
- #include <arpa/inet.h>
- #endif
- #include <netdb.h>
-
- #include <stdio.h>
- #ifdef hpux
- #include <string.h>
- #define index(X,Y) strchr(X,Y)
- #define rindex(X,Y) strrchr(X,Y)
- #else
- #include <strings.h>
- #endif
-
- static char *pgmname;
-
- static int isipname( s )
- char *s;
- {
- pgmname = rindex( s, '/' );
- if( pgmname )
- pgmname++;
- else
- pgmname = s;
- if( !strcmp( pgmname, "ipname" ) )
- return 1;
- if( !strcmp( pgmname, "ipaddr" ) )
- return 0;
- fprintf( stderr, "%s: invoke as ipname or ipaddr\n", pgmname );
- exit( 1 );
- }
-
- main( argc, argv )
- int argc;
- char **argv;
- {
- struct hostent *hp;
- unsigned long addr;
- int i;
- char **ap;
-
- if( isipname( argv[0] ) ) {
- addr = inet_addr( argv[1] );
- hp = gethostbyaddr( (char *) &addr, sizeof( addr ), AF_INET );
- } else
- hp = gethostbyname( argv[1] );
- if( !hp ) {
- fprintf( stderr, "%s: gethostbyname failed\n", pgmname );
- return 1;
- }
- printf( "%s", hp->h_name );
- for( ap = hp->h_aliases; *ap; ap++ )
- printf( " %s", *ap );
- printf( ": " );
- for( i = 0; hp->h_addr_list[i]; i++ )
- printf( "%s ",
- inet_ntoa( * (struct in_addr *) hp->h_addr_list[i] ) );
- printf( "\n" );
- return 0;
- }
- X
- exit 0
-