home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.unix.programmer:5694 comp.sources.wanted:5371 comp.lang.perl:7422
- Path: sparky!uunet!munnari.oz.au!sol.deakin.OZ.AU!mimas.cc.deakin.OZ.AU!not-for-mail
- From: tim@deakin.OZ.AU (Tim Cook)
- Newsgroups: comp.unix.programmer,comp.sources.wanted,comp.lang.perl
- Subject: Re: Get hostname from IP address
- Date: 15 Dec 1992 11:36:31 +1100
- Organization: Deakin University
- Lines: 75
- Message-ID: <1gj9afINN47k@mimas.cc.deakin.OZ.AU>
- References: <Bz2FFM.D4z@avalon.nwc.navy.mil> <1992Dec11.152438.18365@fwi.uva.nl> <1992Dec13.122835.28103@hyphen.com> <1992Dec13.180845.28960@hyphen.com>
- NNTP-Posting-Host: mimas.cc.deakin.oz.au
-
- 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.
-
-
- There Is Always A Simple Perl Script That Will Do It (tm).
-
- The enclosed Perl script will convert a DNS hostname to an IP address
- or vice-versa.
-
-
- #!/usr/local/bin/perl
- # ipq - Answer one of the two common IP queries
- #
- # Tim Cook, December 1992
-
- $prog = substr ($0, rindex ($0, '/') + 1);
-
-
- if ($#ARGV != 0) {
- die "usage: $prog ( hostname | IP-address )\n"; }
-
-
- if ($ARGV[0] =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) {
- &address_to_name ($ARGV[0]); }
- else {
- &name_to_address ($ARGV[0]); }
-
- exit (0);
-
-
-
- sub name_to_address {
- local ($name) = shift (@_);
-
- local (@octets);
-
- local ($nam, $aliases, $addrtype, $length, $address) =
- gethostbyname ($name);
-
- if (! length ($address)) {
- die "$prog: no address found for $name\n"; }
-
- @octets = unpack ("CCCC", $address);
-
- print (join ('.', @octets[0..3]), "\n");
- }
-
-
- sub address_to_name {
- local ($address) = shift (@_);
-
- local (@octets);
- local ($name, $aliases, $type, $len, $addr);
- local ($ip_number);
-
- @octets = split ('\.', $address) ;
-
- if ($#octets != 3) {
- die "$prog: IP-address must have four octets\n"; }
-
- $ip_number = pack ("CCCC", @octets[0..3]);
-
- ($name, $aliases, $type, $len, $addr) = gethostbyaddr ($ip_number, 2) ;
-
- if ($name) {
- print ($name, "\n") ; }
- else {
- die "$prog: no host-name found for $address\n"; }
- }
-