home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.unix.programmer:5665 comp.sources.wanted:5344
- Path: sparky!uunet!spool.mu.edu!agate!doc.ic.ac.uk!uknet!rook.ukc.ac.uk!eagle.ukc.ac.uk!smmat
- From: smmat@ukc.ac.uk (S.M.M.Al-Taher)
- Newsgroups: comp.unix.programmer,comp.sources.wanted
- Subject: Re: Get hostname from IP address
- Message-ID: <2564@eagle.ukc.ac.uk>
- Date: 12 Dec 92 09:45:55 GMT
- References: <Bz2FFM.D4z@avalon.nwc.navy.mil>
- Reply-To: smmat@ukc.ac.uk (S.M.M.Al-Taher)
- Organization: Computing Lab, University of Kent at Canterbury, UK.
- Lines: 57
- Nntp-Posting-Host: eagle.ukc.ac.uk
-
- In article <Bz2FFM.D4z@avalon.nwc.navy.mil> 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.
-
- A while back I also wanted to be able to get host names from the IP address,
- and used the gethostbyaddr function. I've put together the following code:
- -------------------------------------------------------------------------
- /* Get host name from IP address
- * Usage: hostfromip ip-address
- */
-
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
-
- main (argc, argv)
- int argc;
- char **argv;
- {
- char *add;
- unsigned long addr_bin;
- struct hostent *host;
- char *name;
-
- if (argc<2) {
- printf("hostfromip <number_host>\n");
- exit();
- }
- add= argv[1];
- addr_bin= inet_addr(add);
- host= gethostbyaddr((char*) &addr_bin, sizeof(addr_bin),AF_INET);
- name=host->h_name;
- printf("I.P address : %s\nHost Name is : %s\n", add,name);
- }
- -----------------------------------------------------------------------
- Examples:
- % hostfromip 129.12.21.3 (my posting host)
- I.P address : 129.12.21.3
- Host Name is : eagle.ukc.ac.uk
-
- %hostfromip 129.131.31.8 (your host)
- I.P address : 129.131.31.8
- Host Name is : archimedes.nwc.navy.mil
-
- Hope this helps.
- Sami.
- --
- + Sami M M Al-Taher ---------------------------------------------------+
- | Electronic Engineering Labs. University of Kent, Canterbury, Kent, UK. |
- | Tel: +44 227 764000 Ext. 3192 & 3292 (labs) +44 227 769303 (Home) |
- +-------------------------------------------------------------------------+
-