473,372 Members | 1,281 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,372 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 3913
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.