#!/usr/bin/perl -w # tester.pl - test perl constructs # author : dave w capella dave@grox.net # date : Wed Feb 13 10:29:38 PST 2008 ############################################################ use strict; use Sys::Hostname; # for hostname() #use Net::LDAP; #use Net::IP; sub canonical_mac { my $mac = shift or return 0; my @octets = [0,0,0,0,0,0]; my $s = ''; $mac = lc($mac); # lower case only @octets = split(/[:-]/,$mac); if ( $#octets != 5 ) { return ''; } else { foreach $s (@octets) { $s = sprintf("%02x",hex($s)); } return join(':',@octets); } } ############################################################ # is_valid_mac my @maclist = ( '00:e0:3f:09:c1:23', # valid '00:E0:3F:09:C1:23', # valid, uppercase '00E03F09C123', # valid, no separators '0:e0:3f:9:c1:23', # valid, missing zeroes (solaris) '00-e0-3f-09-c1-23', # valid, (microsoft) '00.E0.3F09C123', # invalid. invalid character '.' '00:e03:f:09c:123', # invalid. incorrect format '00:e0:3f:09:c1:23:01' # invalid. wrong length ); my $m = ''; foreach my $s (@maclist) { $m = canonical_mac($s); print "$s\t\t\t$m \n"; } exit; foreach my $s (@maclist) { if( is_valid_mac($s) ) { print "$s is valid \n"; } else { print "$s is NOT valid \n"; } } sub is_valid_mac { my $mac = shift or return 0; # declare our var's. sigh. is this C or perl? # my $debug = 0; my $s = ''; my $x = 0; my @octets = [0,0,0,0,0,0]; $mac = lc($mac); # lower case only if( $mac =~ /[^0-9a-z:-]/ ) { # any invalid characters? print "invalid mac: $mac \n" if $debug; return 0; } if( length($mac) == 12 ){ # no separators: '001122334455'; @octets = unpack('a2a2a2a2a2a2', $mac); } else { # has separators (':', '-') @octets = split(/[:-]/,$mac); } if( $#octets != 5 ) { # sanity check print "invalid mac: $mac \n" if $debug; return 0; } # # overkill. extra sanity check. should not fail. # # # foreach $s (@octets) { # # coerce from string to number # $x = hex($s); # if( $x < 0 or $x > 255 ) { # print "invalid mac: $mac \n" if $debug; # return 0; # } # } print "valid mac: $mac \n" if $debug; return 1; } # is_valid_mac ############################################################ sub authenticate { my ($self, $username, $password) = @_; my $host = "10.0.0.38"; # pa-ad1 my $ldap = new Net::LDAP($host) or die "$@"; my $auth = 0; if ($username !~ /\s/ ) { $username = &getFullname($username); } #my $DN = "OU=Employees,DC=danger,DC=com"; my $DN = "CN=$username,OU=Employees,DC=danger,DC=com"; my $bind = $ldap->bind($DN, password => $password); ###print "LDAP error is ",$bind->code()," that means ",$bind->error(),"\n" if $bind->code(); my $mesg = $ldap->compare( $DN, attr => 'cn', value => $username ); if ( $mesg->code() == 6 && $mesg->error() eq "Compare true") { $auth = 1; } $ldap->unbind(); ## Return 1 if successfully authenticated, 0 otherwise return $auth; } #my $foo = 'foo'; #if( $foo ne 'bar' ) { # print $foo, "\n"; #} #my $foo = 'foo'; my $foo = shift; $foo = 'foo' unless $foo; my $capella = 'capella'; if( $capella =~ $foo ) { print $foo, "\n"; } exit; # won't work with strict # #sub foo { # my $v; # my ($t1, $t2, $t3) = @_; # foreach $v ( $t1, $t2, $t3 ) { # print $v, "\n"; # } #} # #my $t1 = 't1'; #my $t2 = 't2'; #my $t3 = 't3'; # #my @args = ( $t1, $t2, $t3 ); #foo($t1, $t2, $t3); #exit; my @ary = ( 'abc', 'bcd', 'def' ); my $x; foreach $x (@ary) { if ($x eq 'abc') { push @ary,'xyz'; } print $x, "\n"; } #foreach $x (@ary) { # print $x, "\n"; #} #exit; #my $div = ""; #my $len = 0; #my $sep = ""; #$len = length($fqdn); #$sep = "\n"; #$div .= "-" x $len . $sep; #print $div; #sub fqdn2svc { # my $fqdn = shift; # my ($name,@rest) = split('\.',$fqdn); # remove host name # if($#rest - 2) { # remove parent domain # return uc($rest[1] . $rest[0]); # ie, si01.se1(.dngr.org) # } else { # return uc($rest[0]); # ie, ppe(.dngr.org) # } #} # #my @hosts = ( # 'nas1.si01.se1.dngr.org', # 'ppe-ops1.ppe.dngr.org', # '172.20.0.20' #); # #my ($fqdn, $ip); #foreach my $host (@hosts) { # if( $host =~ /^[0-9]/) { # $ip = inet_aton($host); # $fqdn = gethostbyaddr($ip, AF_INET); # if($fqdn) { # print $host, " -> ", fqdn2svc($fqdn), "\n"; # } else { # print "cannot find hostname for $host \n"; # } # } elsif ( $host =~ /(org|com)/ ) { # print $host, " -> ", fqdn2svc($host), "\n"; # } else { # print "is $host really a host? \n"; # } #} # # ##my $mac = "00:01:02:03:04:05"; ##my $query = "values ('meez', '$mac', 'spare')"; ##print $query, "\n"; ## ##my %h = ( '00:00' => 1, '11:11' => 0, '22:22' => 3 ); ##my @ary = keys(%h); ## ##print "array: @ary \n"; ## ##print "------ \n"; ## ##if( $mac =~ /00:/ ) { ## print "yes \n"; ##} else { ## print "no \n"; ##} ## ##sub doit { ## my @ary; ## my $arg; ## do { ## $arg = shift; ## push(@ary, $arg) if $arg; ## } while $arg; ## print "ary: @ary \n"; ## foreach $arg (@ary) { print "arg: $arg \n"; } ##} ## ##print "ary: @ary \n"; ##doit(@ary); ###################################################################### # eof: tester.pl