home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / unix / programm / 5674 < prev    next >
Encoding:
Text File  |  1992-12-13  |  2.3 KB  |  89 lines

  1. Xref: sparky comp.unix.programmer:5674 comp.sources.wanted:5358
  2. Newsgroups: comp.unix.programmer,comp.sources.wanted
  3. Path: sparky!uunet!psinntp!hyphenw!dwight
  4. From: dwight@hyphen.com (Dwight Ernest)
  5. Subject: Re: Get hostname from IP address
  6. Message-ID: <1992Dec13.122835.28103@hyphen.com>
  7. Organization: Hyphen, Inc., Wilmington, MA 01887 USA / +1 508 988-0880
  8. References: <Bz2FFM.D4z@avalon.nwc.navy.mil> <1992Dec11.152438.18365@fwi.uva.nl>
  9. Date: Sun, 13 Dec 1992 12:28:35 GMT
  10. Lines: 77
  11.  
  12. bosman@fwi.uva.nl (Cor Bosman) writes:
  13.  
  14. >dejesus@archimedes.nwc.navy.mil (Francisco X DeJesus) writes:
  15.  
  16. >>I'd like to find a utility I can call from the command-line with an
  17. >>internet IP address as an argument, and have it return to me the full
  18. >>hostname related to it. I've looked into the function gethostbyaddr()
  19. >>but haven't been able to work. I also remember reading about a name-
  20. >>resolver funtion nres_gethostbyaddr() (sp?), but can't find any
  21. >>references to it on my system. I'm running SunOS 4.1.2 on a Sparc.
  22.  
  23. >>Will post a summary if there's interest. Thanks in advance...
  24.  
  25. >Hmm..a command-line program huh...why cant you use:
  26.  
  27. >NSLOOKUP(8C)          MAINTENANCE COMMANDS           NSLOOKUP(8C)
  28.  
  29. >NAME
  30. >     nslookup - query domain name servers interactively
  31.  
  32. Because it's not general enough.  It assumes that BIND is available.
  33. The best thing would be a wrapper around the gethostbyaddr() call.
  34.  
  35. Like this, perhaps:
  36.  
  37.  
  38. /*
  39.  *    gethostbyaddr
  40.  *
  41.  *    For shell scripts and the like.
  42.  *
  43.  *    Given a network IP address, returns the hostname, if it can.
  44.  *
  45.  */
  46.  
  47. #include <stdio.h>
  48. #include <sys/types.h>
  49. #include <sys/socket.h>
  50. #include <netdb.h>
  51. #include <netinet/in.h>
  52.  
  53. void
  54. main (argc, argv)
  55.     int argc;
  56.     char *argv[];
  57. {
  58.     struct hostent *he;
  59.     struct in_addr addr;
  60.     char *prog;
  61.     extern char *basename();
  62.  
  63.     prog = basename (argv[0]);
  64.  
  65.     if (argc != 2)
  66.     {
  67.         fprintf (stderr, "usage: %s address\nreturns main hostname\n", prog);
  68.         exit (1);
  69.     }
  70.  
  71.     addr.s_addr = inet_addr (argv[1]);
  72.  
  73.     if ((he = gethostbyaddr (&addr, 4, AF_INET)) <0)
  74.     {
  75.         fprintf (stderr, "gethostbyaddr() failed\n");
  76.         exit (1);
  77.     }
  78.  
  79.     printf ("%s\n", he->h_name);
  80.     exit (0);
  81. }
  82.  
  83. (basename() is simple and relatively obvious, but let me know if you need it.)
  84.  
  85. -- 
  86. --Dwight A. Ernest   R&D Manager (USA)     I speak only for myself.
  87.   Hyphen, Inc./181 Ballardvale St./Wilmington, MA 01887/+1 508 988 0880 x125
  88.   dwight@hyphen.com
  89.