working with ldap files

F

flit

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=Hatepeople,OU=HR,DC=fabrika,DC=com;CN=pointhairedboss,OU=Groups,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?
 
T

Tim Chase

I have this string on a field
CN=pointhairedpeoplethatsux,OU=Groups,OU=Hatepeople,OU=HR,DC=fabrika,DC=com;CN=pointhairedboss,OU=Groups,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=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(',')], [])
>>> 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
 
T

Tim Chase

I have this string on a field
"CN=pointhairedpeoplethatsux,OU=Groups,OU=Hatepeople,OU=HR,DC=fabrika,DC=com;CN=pointhairedboss,OU=Groups,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
 
?

=?ISO-8859-1?Q?Michael_Str=F6der?=

flit said:
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=Hatepeople,OU=HR,DC=fabrika,DC=com;CN=pointhairedboss,OU=Groups,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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top