http://qs321.pair.com?node_id=269796

ypcat has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I am trying to get Net::LDAP to allow me to send many queries to an LDAP database and return each one of the queries attributes and values to STDOUT as it runs.
Here is what I have tried so far and although it seems to be running every search query, it only writes to the screen the last query results.

For example, my macs.txt contains 10 Mac addresses. When they are sent to LDAP 1 at a time as you will see in the code below only the 10th MAC addresses attributes and values will be returned. Again my goal is to return all attributes and values from every query(all 10 macs).

I appreciate everyone's help. Here is the code.
use Net::LDAP; my $server = "192.168.0.20"; my $port = "389"; my $connection = Net::LDAP->new($server, port => $port) 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 = ['username', 'plan', 'ipaddress']; open MACS, "macs.txt" or die "Couldn't open the file or die: $!\n"; @macs = <MACS>; close MACS; print "Opened the Macs file\n"; my $count=0; foreach my $mac (@macs) { $count++; print "Processing: $count\n"; ldapsearch($mac); } sub ldapsearch { my $mac = shift; print "$mac\n"; my $searchobj = $connection->search( base => 'o=ldap, filter => "cn=$mac", attrs => @attrs, callback => \&callback, ); $searchobj->code && die $searchobj->error; } sub callback { my ($searchobj, $entry) = shift @_; $entry = $searchobj->shift_entry; if ($entry) { print $entry->dn, "\n"; print $entry->get_value('plan'); print $entry->get_value('ipaddress'); print $entry->get_value('username'); } }