Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > working with ldap files

Reply
Thread Tools

working with ldap files

 
 
flit
Guest
Posts: n/a
 
      09-01-2006
Hello All,

I am struggling with some ldap files.

I am using the csv module to work with this files (I exported the ldap
to a csv file).
I have this string on a field
CN=pointhairedpeoplethatsux,OU=Groups,OU=Hatepeopl e,OU=HR,DC=fabrika,DC=com;CN=pointhairedboss,OU=Gr oups,OU=Hatepeople,OU=HR,DC=fabrika,DC=com
this string is all the groups one user has membership.
So what I am trying to do.
read this string
and extract only the CNs

like

pointhairdepeoplethatsux,pointhairedboss

Or think in negative way
remove the OU=****** and DC= ******

Any ideas?

 
Reply With Quote
 
 
 
 
Tim Chase
Guest
Posts: n/a
 
      09-01-2006
> I have this string on a field
> CN=pointhairedpeoplethatsux,OU=Groups,OU=Hatepeopl e,OU=HR,DC=fabrika,DC=com;CN=pointhairedboss,OU=Gr oups,OU=Hatepeople,OU=HR,DC=fabrika,DC=com
> this string is all the groups one user has membership.
> So what I am trying to do.
> read this string
> and extract only the CNs
>
> like
>
> pointhairdepeoplethatsux,pointhairedboss


>>> s =

"CN=pointhairedpeoplethatsux,OU=Groups,OU=Hatepeop le,OU=HR,DC=fabrika,DC=com;CN=pointhairedboss,OU=G roups,OU=Hatepeople,OU=HR,DC=fabrika,DC=com"
>>> pieces = sum([p.split(';') for p in s.split(',')], [])
>>> pieces

['CN=pointhairedpeoplethatsux', 'OU=Groups', 'OU=Hatepeople',
'OU=HR', 'DC=fabrika', 'DC=com', 'CN=pointhairedboss',
'OU=Groups', 'OU=Hatepeople', 'OU=HR', 'DC=fabrika', 'DC=com']
>>> pieces = sum([p.split(';') for p in s.split(',')], [])
>>> cns = [piece[3:] for piece in pieces if piece.startswith('CN=')]
>>> cns

['pointhairedpeoplethatsux', 'pointhairedboss']


The process basically splits on commas, then splits each of those
pieces on semi-colons, then flattens the list-of-lists into a
single list of all the pieces (the flattening is done by abusing
sum() so there may be better, more elegant ways of doing that).
Once you have the flattened list of pieces, you can then just
check for the ones that start with "CN=" and extract the bits of
them that you need.

-tkc




 
Reply With Quote
 
 
 
 
Tim Chase
Guest
Posts: n/a
 
      09-01-2006
>> I have this string on a field
>> CN=pointhairedpeoplethatsux,OU=Groups,OU=Hatepeopl e,OU=HR,DC=fabrika,DC=com;CN=pointhairedboss,OU=Gr oups,OU=Hatepeople,OU=HR,DC=fabrika,DC=com
>> this string is all the groups one user has membership.
>> So what I am trying to do.
>> read this string
>> and extract only the CNs
>>
>> like
>>
>> pointhairdepeoplethatsux,pointhairedboss

>
> >>> s =

> "CN=pointhairedpeoplethatsux,OU=Groups,OU=Hatepeop le,OU=HR,DC=fabrika,DC=com;CN=pointhairedboss,OU=G roups,OU=Hatepeople,OU=HR,DC=fabrika,DC=com"


Or, if you're a regexp junkie...

>>> import re
>>> r = re.compile('CN=([^;,]*)')
>>> r.findall(s)

['pointhairedpeoplethatsux', 'pointhairedboss']

I'll leave timing comparisons as an exercise to the reader...

Both of these solutions make the assumption that neither a comma
nor a semicolon are permissible in a CN.

-tkc



 
Reply With Quote
 
=?ISO-8859-1?Q?Michael_Str=F6der?=
Guest
Posts: n/a
 
      09-04-2006
flit wrote:
>
> I am struggling with some ldap files.


More general you are struggling with multiple attribute values of DN
syntax stored in a single field of a CSV file.

> I am using the csv module to work with this files (I exported the ldap
> to a csv file).


I guess you have MS AD and used MS tools for CSV export.

> I have this string on a field
> CN=pointhairedpeoplethatsux,OU=Groups,OU=Hatepeopl e,OU=HR,DC=fabrika,DC=com;CN=pointhairedboss,OU=Gr oups,OU=Hatepeople,OU=HR,DC=fabrika,DC=com
> this string is all the groups one user has membership.


It seems they are using ; as a delimiter for multi-valued attributes in
a single CSV field. Note that ; is also a special character for
LDAPv2-DNs. So a naive parsing will fail under special circumstances.

I'd recommend to export your data as LDIF and use the module 'ldif' from
python-ldap to extract the entry records. You can use this module
separately simply by placing the file ldif.py under site-packages/ if
you don't need the rest of python-ldap.

> So what I am trying to do.
> read this string
> and extract only the CNs


This is another issue.

Note that in general DN parsing is more complex than simply using
string.split(). If you are sure that all the attribute values used in
your DNs don't have any special chars you could use string.split(). But
you should definitely cross-check with RFC 4514 or use a decent DN parser.

Ciao, Michael.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
python-ldap/win32 or python/ldap/win32 rcmn Python 1 11-06-2006 11:47 PM
Need to wrtie LDAP class to *answer* LDAP queries. junk1@davidbevan.co.uk Java 1 02-21-2006 09:59 PM
ruby-ldap: uninitialized constant LDAP::LDAP_CONTROL_PAGEDRESULTS James Hughes Ruby 4 12-13-2005 11:46 PM
[ANN] Ruby/LDAP 0.9.1: LDAP API (RFC1823) library. Ian Macdonald Ruby 0 03-15-2005 11:23 PM
using LDAP Controls in ruby-ldap Jason Wold Ruby 5 11-07-2004 03:35 AM



Advertisments