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

  1. Xref: sparky comp.unix.programmer:5665 comp.sources.wanted:5344
  2. Path: sparky!uunet!spool.mu.edu!agate!doc.ic.ac.uk!uknet!rook.ukc.ac.uk!eagle.ukc.ac.uk!smmat
  3. From: smmat@ukc.ac.uk (S.M.M.Al-Taher)
  4. Newsgroups: comp.unix.programmer,comp.sources.wanted
  5. Subject: Re: Get hostname from IP address
  6. Message-ID: <2564@eagle.ukc.ac.uk>
  7. Date: 12 Dec 92 09:45:55 GMT
  8. References: <Bz2FFM.D4z@avalon.nwc.navy.mil>
  9. Reply-To: smmat@ukc.ac.uk (S.M.M.Al-Taher)
  10. Organization: Computing Lab, University of Kent at Canterbury, UK.
  11. Lines: 57
  12. Nntp-Posting-Host: eagle.ukc.ac.uk
  13.  
  14. In article <Bz2FFM.D4z@avalon.nwc.navy.mil> dejesus@archimedes.nwc.navy.mil (Francisco X DeJesus) writes:
  15. >I'd like to find a utility I can call from the command-line with an
  16. >internet IP address as an argument, and have it return to me the full
  17. >hostname related to it. I've looked into the function gethostbyaddr()
  18. >but haven't been able to work. I also remember reading about a name-
  19. >resolver funtion nres_gethostbyaddr() (sp?), but can't find any
  20. >references to it on my system. I'm running SunOS 4.1.2 on a Sparc.
  21.  
  22. A while back I also wanted to be able to get host names from the IP address,
  23. and used the gethostbyaddr function. I've put together the following code:
  24. -------------------------------------------------------------------------
  25. /*  Get host name from IP address
  26.  *  Usage: hostfromip ip-address
  27.  */
  28.  
  29. #include <sys/types.h>
  30. #include <sys/socket.h>
  31. #include <netdb.h>
  32. #include <netinet/in.h>
  33. #include <arpa/inet.h>
  34.  
  35. main (argc, argv)
  36. int argc;
  37. char **argv;
  38. {
  39.    char *add;
  40.    unsigned long addr_bin;
  41.    struct hostent *host;
  42.    char *name;
  43.  
  44.    if (argc<2) { 
  45.       printf("hostfromip <number_host>\n");
  46.       exit();
  47.    }
  48.    add= argv[1];
  49.    addr_bin= inet_addr(add);
  50.    host= gethostbyaddr((char*) &addr_bin, sizeof(addr_bin),AF_INET);
  51.    name=host->h_name;
  52.    printf("I.P address  : %s\nHost Name is : %s\n", add,name);
  53. }
  54. -----------------------------------------------------------------------
  55. Examples:
  56. % hostfromip 129.12.21.3 (my posting host)
  57. I.P address  : 129.12.21.3
  58. Host Name is : eagle.ukc.ac.uk
  59.  
  60. %hostfromip 129.131.31.8 (your host)
  61. I.P address  : 129.131.31.8
  62. Host Name is : archimedes.nwc.navy.mil
  63.  
  64. Hope this helps.
  65. Sami.
  66. --
  67. + Sami M M Al-Taher    ---------------------------------------------------+ 
  68. | Electronic Engineering Labs. University of Kent, Canterbury, Kent, UK.  |
  69. | Tel: +44 227 764000 Ext. 3192 & 3292 (labs)   +44 227 769303 (Home)     |
  70. +-------------------------------------------------------------------------+
  71.