Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



Network Programming with Perl    (ISBN: 0201615711)


 

 List Price: $44.95
 Our Price: $44.95
 Used Price: $22.49

 Release Date: 27 December, 2000
 Manufacturer: Addison-Wesley Pub Co (Paperback)
 Sales Rank: 63,530

 Author: Lincoln D. Stein









More Info

 Hostname Lookup 2002-03-12 21:54:57
  perl 
Category: source:perl:internet
Description: Resolves a hostname into an ip address or an ip address into a hostname.
Platform: unix
Author: detour
Viewed: 5289
Rating: 4.7/5 (32 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
resolv.pl
#!/usr/bin/perl
# resolv.pl written by detour@metalshell.com
#
# Resolves an ip into a host or a host into an ip.
#
# http://www.metalshell.com/
#

use Socket;
use strict;

my $host_name = hostname($ARGV[0]);
print "$ARGV[0] resolves to $host_name\n";

sub hostname {

  my (@bytes, @octets,
    $packedaddr,
    $raw_addr,
    $host_name,
    $ip
  );

  if($_[0] =~ /[a-zA-Z]/g) {
    $raw_addr = (gethostbyname($_[0]))[4];
    @octets = unpack("C4", $raw_addr);
    $host_name = join(".", @octets);
  } else {
    @bytes = split(/\./, $_[0]);
    $packedaddr = pack("C4",@bytes);
    $host_name = (gethostbyaddr($packedaddr, 2))[0];
  }

  return($host_name);
}
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?