working with ldap files

Discussion in 'Python' started by flit, Sep 1, 2006.

  1. flit

    flit Guest

    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?
     
    flit, Sep 1, 2006
    #1
    1. Advertising

  2. flit

    Tim Chase Guest

    > 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
     
    Tim Chase, Sep 1, 2006
    #2
    1. Advertising

  3. flit

    Tim Chase Guest

    Re: working with ldap files (2nd answer)

    >> 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"


    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
     
    Tim Chase, Sep 1, 2006
    #3
  4. 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=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.
     
    =?ISO-8859-1?Q?Michael_Str=F6der?=, Sep 4, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    556
    Raymond DeCampo
    Feb 21, 2006
  2. rcmn
    Replies:
    1
    Views:
    375
    =?ISO-8859-1?Q?Michael_Str=F6der?=
    Nov 6, 2006
  3. Jason Wold

    using LDAP Controls in ruby-ldap

    Jason Wold, Nov 4, 2004, in forum: Ruby
    Replies:
    5
    Views:
    282
  4. Ian Macdonald
    Replies:
    0
    Views:
    239
    Ian Macdonald
    Mar 15, 2005
  5. James Hughes
    Replies:
    4
    Views:
    352
    James Hughes
    Dec 13, 2005
Loading...

Share This Page