Perl module chkIPadr

From Teknologisk videncenter
Revision as of 08:50, 31 March 2010 by Heth (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
#Filnavn: ip.pm
package ip;
use base 'Exporter';
our @EXPORT_OK = qw(chkIPadr);
use strict;
use warnings;
sub chkIPadr {
  my $ip = shift;
  if ( $ip =~ m/(^[1-9][0-9]*)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/ ) {
    if (!(($1<224)&($2<256)&($3<256)&($4<256))) {
      return(0);
    } else {
      return($ip);
    }
  }
  #Hvis det ikke er en IP adresse er det måske et DNS-navn
  if ( my $packedIP=gethostbyname( $ip )) {
     my $realIP = inet_ntoa($packedIP);
     return($realIP);
  } else {
    return(0);
  }
}
1; #Et modul skal afslutte sandt (1)