Perl module chkIPadr

From Teknologisk videncenter
Revision as of 13:51, 9 November 2009 by Heth (talk | contribs) (New page: <source lang=perl> #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]*)...)
(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)