Perl and Net::LDAP, objects and arrays query


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl and Net::LDAP, objects and arrays query
# 1  
Old 08-21-2008
Perl and Net::LDAP, objects and arrays query

Hi

I'm not a programmer but am muddling through as best I can. I am trying to set up a PostSearchHook for Radiator (RADIUS server), that carries out an LDAP lookup, and, based on the
string returned ("staff" or "student") in the "businessCategory" attribute, will set the $role to be either 40 or 20 (a VLAN ID).

There can however be multiple attributes for "businessCategory" (I for example, have "staff" and "imageportaladmin"), but the only string values needed are "staff" or "student", a
nd if both are returned, then "staff" must take precedent.

The code I have so far:

Code:
#!/usr/bin/perl -w
use diagnostics;
use Net::LDAP;
my $role = "";
$ldap = Net::LDAP->new("ldap://ldaphost");
$ldap->bind();
$mesg = $ldap->search(base => "ou=people,dc=base",
                        scope => "1",
                        filter => "(uid=MLott)",
                        attrs => ['businessCategory'],
                        );

                                        
# unbind from LDAP cleanly
$mesg = $ldap->unbind();

What I'm not clear on is how I retrieve the data in the $mesg object. If I add the following code above the unbind() method:

Code:
foreach $entry (@entries) {
        $entry->dump;
}

I get:

Code:
------------------------------------------------------------------------
dn:uid=MLott,ou=People,dc=base

businessCategory: staff
                  imageportaladmin

However, I need to be able to query this data for the correct strings (that I mentioned earlier). I know that dump() is method of Net::LDAP, but it shows that the query result is
correct.

If I add the following lines so that I can get a count, I only get the result of "1":

Code:
foreach $max ($mesg->count) {
        print "Count = $max\n";
}

Is that because it is "1" object returned (as opposed to items in a scalar list)?

Any help would be great.

Thanks,

Mike
# 2  
Old 08-21-2008
Phew, it's been quite some time since I last played with Net::LDAP.
And as far as I can remember it took quite a bit of reading the POD beforehand.
So I would suggest you revisit the POD because it is all explained there how you access the returned objref of Net::LDAP::Search.
Because I have forgotten almost all I just have had a quick glance at it on search.cpan.org,
Net::LDAP::Search - Object returned by Net::LDAP search method - search.cpan.org
This class offers several accessor methods to fetch the result set.
As you went for the entries() method, this it says returns an array of Net::LDAP::Entry objrefs.
So you would have to look at this class' POD as well to know how to access the data
Net::LDAP::Entry - An LDAP entry object - search.cpan.org
Just from my very superficial exposure to the POD right now I would probably try something like this
(please, note this is a mere wild guess since I haven't even Net::LDAP installed on my surf laptop)
Code:
foreach my $entry ($mesg->entries()) {
    my @vals = grep { /staff|student/ } @{ $entry->get_value('businessCategory', asref => 1) };
    # further process @vals here
}

But please, this is a wild untested guess which probably is horribly wrong.
You just will have to consult the POD.
What I usually do to get a quick view of the deeply nested data structures I am not yet clear about
is to run my code in the Perl debugger (just invoke your script with preceeding perl -d
Then I continue (c) to the line where the result is fetched and view it in my pager by
Code:
DB<3> | x $mesg

This is most of the times far quicker to grasp then perusing the whole POD.
# 3  
Old 08-21-2008
Hey buffoonix

Thanks for the effort. I've been scouring the POD all day Smilie Like, I say, I'm not a programmer so this is all tough to take in but I guess that is half the fun.

Thanks for the tip on the Perl debugger; that's very helpful and I'll give that a try together with the POD and maybe I'll come up trumps with it.

Thanks,
Mike
# 4  
Old 08-22-2008
Well, I figured it out (I think), so this is the code I'm using and it seems to work. Am fully open to someone checking it out and pointing out better ways of achieving the same thing though:

Code:
while( my $entry = $mesg->shift_entry) {
	my @values = $entry->get_value( 'businessCategory' );
       #print "@values\n";    
	if ( grep ( /staff$/i, @values ) ) { $role = '40'; }
	elsif ( grep ( /student$/i, @values ) ) { $role = '20'; }
	elsif ( grep ( /left$/i, @values ) ) { exit 0; }
  	}

Mike
This User Gave Thanks to mikie For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

LDAP Query - host allowed option

I have an in interesting dilemna that I am trying to address. I have some ldap queries that I use to retrieve user information to perform access validations on a quarterly/annual basis. I can successfully pull the local users, and I can use ldapsearch to pull back all the users from the DN as well.... (7 Replies)
Discussion started by: dagamier
7 Replies

2. UNIX and Linux Applications

LDAP Group query

I need to write LDAP group query where I need to find if a particular user is a member of a 2 specific Groups. This is LDAP Novell edirectory implementation. Below are the details - ================ LDIF entry for OndotAPI group dn: cn=OndotAPI,ou=Groups,o=CNS changetype: add ... (0 Replies)
Discussion started by: jhamaks
0 Replies

3. Emergency UNIX and Linux Support

LDAP and AD Authentication Query

Hi Friends, I have below scenarios . dom1.test.com - LDAP dom2.test.com - AD Requirement is establish a trust relation between LDAP and AD server in such a way that if any user login on LDAP managed authentication server with dom1\username -> get authenticated by LDAP host ... (2 Replies)
Discussion started by: Shirishlnx
2 Replies

4. Shell Programming and Scripting

Ldap search query

Hi All, I have a existing Ldap query which take a HOME as variable and gives the result where i grep for a particular line. ldapsearch -h server_domain_name -p 389 -D "uid=user,ou=appadm,o=ent" -w PaB -b "ou=roles,o=ent" "cidx=$HOME" | grep -w "ent: xyz" Now i have 330K Homes in a... (1 Reply)
Discussion started by: posner
1 Replies

5. Shell Programming and Scripting

perl - return an object from subroutine - Net::LDAP

Hi all, I'm not even sure a person can do this in perl, seems like you should be able to though. Here's the error IO::Socket::INET: connect: Operation now in progress at server_search.pl line 256, <DATA> line 466. Here's the perl code... sub ldap_new{ $nl = Net::LDAP->new( "$_" ) or... (3 Replies)
Discussion started by: jtollefson
3 Replies

6. UNIX for Dummies Questions & Answers

Awk - query about arrays

Hello again, I have example config file with two arrays: tab1="name1 surname1" tab1="name2 surname2" tab1="name3 surname3" tab2="First" tab2="Second"and csv file: "aaaaa","surname1","name1","ddddd,eeeee","ffffff","ggggg","3","2012/02/22 12:25:21","2012/02/22... (7 Replies)
Discussion started by: haczyk
7 Replies

7. Shell Programming and Scripting

PERL: Replace multiple objects within a string

looking to replace parameters within a string with an external answer - with multiple replacements within a string %% will be used to wrap the objects to be replaced i.e. hello %%title%% %%user%% from %%address%% you last %%action%% on %%object%% the params will be used to make calls to a... (1 Reply)
Discussion started by: bidbrooken
1 Replies

8. Solaris

Net-SNMP v5.4 - sub-id not found in log, but can query with snmpget and snmpwalk

Hi everyone, I'm configuring Net-SNMP v5.4 on Solaris 10, sending traps to OpenNMS 1.2.9-0.20061212.0. I'm setting up disk monitoring (% of space free) for 5 directories. Here's what I'm working with: (snmpd.conf - disk and monitoring portions) # # Disk Usage monitoring: # disk / 20%... (0 Replies)
Discussion started by: Mariognarly
0 Replies

9. UNIX for Dummies Questions & Answers

LDAP search query help

I would like to do an ldap search which looks for entries which do not actually have a certain attribute. Not that the attribute is Null, but where the attribute does not exist. Is this possible using ldapsearch? (3 Replies)
Discussion started by: dopple
3 Replies

10. Solaris

New to Perl-LDAP

Hi all I am new to perl-ldap. Trying to install DBD module on sun solaris which has the iplanet51 ldap already installed. I have got the DBD-LDAP-0.03.tar file , and un packed it. From the dir DBD-LDAP-0.03 - i tried to do $ perl Makefile.PL and i got the... (10 Replies)
Discussion started by: kaushi
10 Replies
Login or Register to Ask a Question