473,594 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=Use rs,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_vers ion = ldap.VERSION3
l.simple_bind(u sername, passwd)
filter = '(objectClass=i pHost)'
attrs = ['cn','ipHostNum ber','descripti on']
raw_res = l.search_s( basedn, ldap.SCOPE_SUBT REE, filter, attrs )
except ldap.INVALID_CR EDENTIALS:
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_r es)):
print I, ": ",

l.unbind()
Oct 11 '08 #1
2 3929
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=Use rs,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(ent ry):
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(valu e)
print

the ", ".join(valu e) 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_SUBT REE, filter, attrs )
This is the synchronous method which might not be suitable for large
result sets.
for I in range(len(raw_r es)):
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,StringTyp e 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=T esting,dc=...', ldap.SCOPE_ONEL EVEL,attrlist=['cn']))
[('uid=anna,ou=T esting,dc=stroe der,dc=de',
{'cn': ['Anna Blume']}),
('cn=Fred Feuerstein,ou=T esting,dc=stroe der,dc=de',
{'cn': ['Fred Feuerstein']}),
(None, ['ldap://ldap.openldap.o rg/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
1415
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 & all the other things about the inetOrgPerson entries, but i'm abit at a loss how to obtain the resource pointing to the entry i need. Does anyone know some good docs on this? the manual isn't really helping me
5
3264
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 list. assume the string is in the variable f, then i tried f.split() but i end up with
0
1405
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 options that I have seen for doing this are to either use scripts to update either periodically checks constancy or scans the logs for changes. The other option is to use some thrid party "metadirectory" solution. Anyone have any insight on how...
7
6791
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 credentials. What i am trying to do is, i am trying to look up all the information for the user with user id 'testuser'. The following is the Vb.net code for my aspx page: Dim oRoot As DirectoryEntry = New...
19
10906
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 empty strings in the resulting string array.
9
2182
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 that has problems. FullName=Request.Form("Name") Email=Request.Form("Email") GivenName=Request.Form("GivenName")
1
1457
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 give me a help!!! cause we need it as soon as possible..
0
3209
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 given all the attributes which are needed, for the user, in the code and also the proper path where the user has to be added. Please have a look at my code CODE] // This is a class file which stores all the info required for the user
0
1839
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 now I get this error. Is there anyway to avoid building the python_ldap binaries? Apart from being lazy, I've got a secure system policy issue if I start compiling apps. I could give up and just start running in linux, but myxp environment is...
0
7946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7877
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8253
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8240
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
3867
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1482
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1216
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.