473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Split entries from LDAP

Hi
I got some programming experience and I recently started looking into
Python. I've read much of the tutorial from 2.6 documentation. But it
was more interesting to get started on something I needed. I'm trying
to create a script that creates a variable list (just a txt file to be
included in bash scripts) with hosts from LDAP. The file will include
some static entries and the hosts based on 'cn', 'ipHostNumber' and I
might as well set the 'description' as commen,t when the list from
LDAP is created.
I got all the entries in the variable "raw_res" and I now got some
doubts on how to split each value up in every entry. Belove you can
see one of entries printed from the loop.
cn=world.dom.dk,ou=Hosts,o=Users,dc=dom,dc=dk', {'ipHostNumber':
['192.168.0.43'], 'cn': ['world.dom.dk'], 'description':
['Mail&webserver']})"

I've tried different things, but don't quite know to split the tuple.
The examples I've seen is with a nice clean list ("a", "b", "z"), and
mine is full of special characters and etc., so some direction would
be appreciated.
/Lars
----------------------------------------------
#!/usr/bin/env python

import ldap, sys, ldif

# .: LDAP Connection Settings :.
server="NA"
username="NA"
passwd="NA"
basedn="NA"

try:
l = ldap.initialize(server)
l.protocol_version = ldap.VERSION3
l.simple_bind(username, passwd)
filter = '(objectClass=ipHost)'
attrs = ['cn','ipHostNumber','description']
raw_res = l.search_s( basedn, ldap.SCOPE_SUBTREE, filter, attrs )
except ldap.INVALID_CREDENTIALS:
print "Your username or password is incorrect."
sys.exit()
except ldap.LDAPError, e:
print e
sys.exit()

#print raw_res

for I in range(len(raw_res)):
print I, ": ",

l.unbind()
Oct 11 '08 #1
2 3916
Lars schrieb:
I got all the entries in the variable "raw_res" and I now got some
doubts on how to split each value up in every entry. Belove you can
see one of entries printed from the loop.
cn=world.dom.dk,ou=Hosts,o=Users,dc=dom,dc=dk', {'ipHostNumber':
['192.168.0.43'], 'cn': ['world.dom.dk'], 'description':
['Mail&webserver']})"

I've tried different things, but don't quite know to split the tuple.
search_s() returns a tuple of lenght 2. The first entry is the DN, the
second entry is a dictionary with attributes as keys and lists of values
as values. Possible function to handle this (untested):

def print_entry(entry):
print "Got Entry for DN: %s" % entry[0]
print "Attributes:"
for key, value in entry[1].items():
print "\tKey: %s" % key
print "\tValue(s): %s" ", ".join(value)
print

the ", ".join(value) creates a string from a list, check the docs for
dictionaries for other syntax elements.

cheers
Paul

Oct 12 '08 #2
Lars wrote:
I'm trying
to create a script that creates a variable list (just a txt file to be
included in bash scripts) with hosts from LDAP.
What exactly do you want to do? I'd recommend against passing a custom
text format around. Use either LDIF or CSV with decent modules.
The file will include
some static entries and the hosts based on 'cn', 'ipHostNumber' and I
might as well set the 'description' as commen,t when the list from
LDAP is created.
Better give an example.
I got all the entries in the variable "raw_res" and I now got some
doubts on how to split each value up in every entry.
raw_res = l.search_s( basedn, ldap.SCOPE_SUBTREE, filter, attrs )
This is the synchronous method which might not be suitable for large
result sets.
for I in range(len(raw_res)):
print I, ": ",
In a simple case you could do:

for dn,entry in raw_res:
print dn,entry # or whatever

But in LDAPv3 search results can also be search continuation (or
sometimes called referrals). E.g. AD makes use of them when search from
the domain level (without subordinate ou). In python-ldap these are
returned as a 2-tuple (None,StringType with LDAP URL).

So be prepared to handle the case that in the example above dn is None
and entry is a LDAP URL pointing to another server or part of the DIT.

On my test server:
>>>
pprint.pprint(l.search_s('ou=Testing,dc=...',ldap. SCOPE_ONELEVEL,attrlist=['cn']))
[('uid=anna,ou=Testing,dc=stroeder,dc=de',
{'cn': ['Anna Blume']}),
('cn=Fred Feuerstein,ou=Testing,dc=stroeder,dc=de',
{'cn': ['Fred Feuerstein']}),
(None, ['ldap://ldap.openldap.org/dc=openldap,dc=org??base'])]

Ciao, Michael.
Oct 13 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ochal Christophe | last post by:
Hi all, I'm currently trying to make a webbased addressbook thingie, i've managed to get it to search through existing addresses, but i was wondering how i can modify them. I know the dn &...
5
by: oliver | last post by:
hi there i'm experimanting with imaplib and came across stringts like (\HasNoChildren) "." "INBOX.Sent Items" in which the quotes are part of the string. now i try to convert this into a...
0
by: J.Eddy | last post by:
I want to copy LDAP user entries from my LDAP to my MS SQL instance. But I also want them to be synchronized too. If the user is added to the LDAP the SQL DB is changed in lockstep. The olny two...
7
by: Amar | last post by:
I am trying to connect to my college LDAP directory using ASP.NET. This LDap does not have security as it returns only user demographic information. i do not need to bind with a username or...
19
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
9
by: MrHelpMe | last post by:
Hello again experts, I have successfully pulled data from an LDAP server and now what I want to do is drop the data into a database table. The following is my code that will insert the data but...
1
by: hamarsheh | last post by:
thanks alot Motorna!!! iam exploring you'r link for php.net , but till i find my important code ,please if any one has acode that he tried and worked with him about reading ldap entries by php ...
0
by: rbukkara | last post by:
Hi, I have got the following error while trying to add a user in the LDAP Directory. javax.naming.NameNotFoundException: ; remaining name 'uid=vassila,ou=People,dc=cs,dc=uno,dc=edu' I have...
0
by: Sells, Fred | last post by:
I'm running python 2.5 (or 2.4) in an XP environment. I downloaded and installed the .dll's from OpenLDAP-2.4.8+OpenSSL-0.9.8g-Win32.zip and copied the .dll's in c:/windows/system32 as instructed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.