Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use Text::Shellwords;
- use Getopt::Long;
- use Net::LDAP;
- my %opts = ();
- $|=1;
- GetOptions(
- "H:s"=>\$opts{H},
- "T:i"=>\$opts{T},
- "debug"=>\$opts{debug},
- "D:s"=>\$opts{D},
- "B=s"=>\$opts{B},
- "w:s"=>\$opts{w},
- "M:s"=>\$opts{M}
- );
- if (!$opts{T}){
- $opts{T} = 30*60;
- }
- my $timeOut = $opts{T};
- if (!$opts{H}) {
- $opts{H} = '127.0.0.1';
- }
- if (!$opts{M}){
- $opts{M} = $opts{H};
- }
- if ($opts{debug}){
- open (DEBUG, ">&STDERR");
- print DEBUG "DEBUG: Start\n";
- print DEBUG "DEBUG: timeOut: [$timeOut]\n";
- }
- my $ldap;
- $ldap = Net::LDAP->new( $opts{H} ) or die "$@";
- print DEBUG "DEBUG: LDAP Connected " . $opts{H} . "\n" if ($opts{debug});
- my $mesg;
- if (defined($opts{D})){
- $mesg = $ldap->bind( $opts{D},
- password => $opts{w}
- );
- }
- else{
- $mesg = $ldap->bind;
- }
- while (<>) {
- ($ip,$name) = &shellwords($_);
- print DEBUG "DEBUG: ip: [$ip] name: [$name]\n" if $opts{debug};
- if (&valid($ip,$name)) {
- print "OK\n";
- } else {
- print "ERR\n";
- }
- }
- $mesg = $ldap->unbind;
- $ldap->disconnect();
- close DEBUG;
- sub valid {
- my $ip = shift;
- my $name = shift;
- $mesg = $ldap->search(base => $opts{B},
- scope => "sub",
- filter => "cn=" . $name,
- attrs => ['cn','ipHostNumber','l']);
- my $found = 0;
- my $ee;
- foreach my $entry ($mesg->entries) {
- my $e = $entry->get_value('cn');
- if ($opts{debug}){
- #$entry->dump if $opts{debug};
- print DEBUG "DEBUG: [$e]\n";
- }
- if (lc($e) eq lc($name)){
- $found |= 1;
- $ee = $entry;
- print DEBUG "DEBUG: Found!\n" if $opts{debug};
- }
- }
- my $uip; my $uname; my $udate;
- if ($found){
- $udate = $ee->get_value('l');
- $uip = $ee->get_value('ipHostNumber');
- $uname = $ee->get_value('cn');
- print DEBUG "udate: [$udate]\n" if $opts{debug};
- print DEBUG "uip: [$uip]\n" if $opts{debug};
- print DEBUG "uname: [$uname]\n" if $opts{debug};
- print DEBUG time() . "- $udate = " . (time() - $udate) . "\n" if $opts{debug};
- if (((time() - $udate) <= $timeOut) and ($ip ne $uip)){
- return 0;
- }
- # Actualiza
- &update ($name,$ip,time());
- return 1;
- }
- return 0;
- }
- sub update {
- my $n = shift;
- my $i = shift;
- my $d = shift;
- my $mesg2;
- my $ldap2;
- $ldap2 = Net::LDAP->new( $opts{M} ) or return 0;
- print DEBUG "Connected to Master " . $opts{M} . "\n" if $opts{debug};
- if ($opts{D}){
- $mesg2 = $ldap2->bind( $opts{D},
- password => $opts{w}
- );
- }
- else{
- $mesg2 = $ldap2->bind;
- }
- print DEBUG "Binded\n" if $opts{debug};
- $mesg2 = $ldap2->modify ("cn=$n," . $opts{B},
- changes => [
- replace => [ 'ipHostNumber' => $i,
- 'l' => $d]
- ]
- );
- print DEBUG "Modified ipHostNumber => $i, l => $d\n" if $opts{debug};
- $mesg2 = $ldap2->unbind;
- $ldap2->disconnect();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement