home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.unix.programmer:5674 comp.sources.wanted:5358
- Newsgroups: comp.unix.programmer,comp.sources.wanted
- Path: sparky!uunet!psinntp!hyphenw!dwight
- From: dwight@hyphen.com (Dwight Ernest)
- Subject: Re: Get hostname from IP address
- Message-ID: <1992Dec13.122835.28103@hyphen.com>
- Organization: Hyphen, Inc., Wilmington, MA 01887 USA / +1 508 988-0880
- References: <Bz2FFM.D4z@avalon.nwc.navy.mil> <1992Dec11.152438.18365@fwi.uva.nl>
- Date: Sun, 13 Dec 1992 12:28:35 GMT
- Lines: 77
-
- bosman@fwi.uva.nl (Cor Bosman) writes:
-
- >dejesus@archimedes.nwc.navy.mil (Francisco X DeJesus) writes:
-
- >>I'd like to find a utility I can call from the command-line with an
- >>internet IP address as an argument, and have it return to me the full
- >>hostname related to it. I've looked into the function gethostbyaddr()
- >>but haven't been able to work. I also remember reading about a name-
- >>resolver funtion nres_gethostbyaddr() (sp?), but can't find any
- >>references to it on my system. I'm running SunOS 4.1.2 on a Sparc.
-
- >>Will post a summary if there's interest. Thanks in advance...
-
- >Hmm..a command-line program huh...why cant you use:
-
- >NSLOOKUP(8C) MAINTENANCE COMMANDS NSLOOKUP(8C)
-
- >NAME
- > nslookup - query domain name servers interactively
-
- Because it's not general enough. It assumes that BIND is available.
- The best thing would be a wrapper around the gethostbyaddr() call.
-
- Like this, perhaps:
-
-
- /*
- * gethostbyaddr
- *
- * For shell scripts and the like.
- *
- * Given a network IP address, returns the hostname, if it can.
- *
- */
-
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <netinet/in.h>
-
- void
- main (argc, argv)
- int argc;
- char *argv[];
- {
- struct hostent *he;
- struct in_addr addr;
- char *prog;
- extern char *basename();
-
- prog = basename (argv[0]);
-
- if (argc != 2)
- {
- fprintf (stderr, "usage: %s address\nreturns main hostname\n", prog);
- exit (1);
- }
-
- addr.s_addr = inet_addr (argv[1]);
-
- if ((he = gethostbyaddr (&addr, 4, AF_INET)) <0)
- {
- fprintf (stderr, "gethostbyaddr() failed\n");
- exit (1);
- }
-
- printf ("%s\n", he->h_name);
- exit (0);
- }
-
- (basename() is simple and relatively obvious, but let me know if you need it.)
-
- --
- --Dwight A. Ernest R&D Manager (USA) I speak only for myself.
- Hyphen, Inc./181 Ballardvale St./Wilmington, MA 01887/+1 508 988 0880 x125
- dwight@hyphen.com
-