#!/usr/bin/perl
####################################################################
#Author: Jason Riedel                                              #
#Email: jasonriedel@jasonriedel.com								   #
####################################################################


use strict;

use vars qw /@macs/;

use Net::LDAP;

my $server = "ldapip";
my $port = "389";

my $connection = Net::LDAP->new($server, port => $port, debug => 12) or die "Unable to connect to $server:", @$, "\n";

$connection->bind() or die "could not bind: $@\n";
print "\n\nConnected to LDAP\n\n";

my @attrs = ['mapsaccount', 'clientclass', 'ipaddress'];

open NEWMACS, "cablemodems.txt" or die "Couldn't open the file or die: $!\n";
@macs = <NEWMACS>;
close NEWMACS;

print "Opened the Macs file\n";

passer();

sub passer {
	my $count=0;
	foreach my $mac (@macs) {
		## Formatting the Mac for LDAP ##
		$mac =~ s/\.//g;
		$mac =~ s/(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})/$1:$2:$3:$4:$5:$6/;
		$count++;		
		print "Processing: $count\n";
		ldapsearch($mac);
	}
}

sub ldapsearch {
		my $mac = shift;
		print "$mac\n";
		my $searchobj = $connection->search(
										base	=> 'o=cableone',
										scope	=> 'sub',
										filter	=> "cn=01:$mac",
										attrs	=> @attrs,
										callback => \&callback,
									);
		$searchobj->code && die $searchobj->error;
}

sub callback {
	
	my ($searchobj, $entry) = @_;
	
#	my $number = $searchobj->count;
	$entry = $searchobj->shift_entry;
	if ($entry) {
		print $entry->dn, "\n";
		print $entry->get_value('clientclass');
		print $entry->get_value('ipaddress');
		print $entry->get_value('mapsaccount');
	}
	else {
		warn "No passed entry in for callback\n\n";
	}
}

$connection->unbind(); ## Close the connection to LDAP