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() 2 3811
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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 &...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 ...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
| |