Split entries from LDAP

Lars nogetfx at gmail.com
Sat Oct 11 10:46:19 EDT 2008


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()



More information about the Python-list mailing list