I've written the following script in python with python-ldap to pull accounts from Active Directory using ldap.
The idea is that it will run nightly in cron and create any acounts I need on a zimbra mailserver (it can be easily modified to do other stuff - and should run ok on mac/*nix/windows - not for zimbra obviously)
It all seems to work except for one bit - Zimbra needs me to run the command
the command has 2 single quotes (blank password because I authenticate to AD) and this is where I'm stuck - I can easily escape the single quotes if I'm using the print command eg:
zmprov ca username@domain '' displayname name
but not if I use
>>> password = "\'\'"
>>> print password
... I gust get blanks for the password and no quotes, I also tried triplequoting
os.system('echo command %s ' % (password))
Any help appreciated.
# This file is covered by GNU GPL.
import ldap, string, os
domain = "example.com"
l=ldap.initialize("ldap://server.example.com":389") # pick an LDAP server
res = l.search_s('ou=students,dc=example,dc=com',
ldap.SCOPE_SUBTREE, "(&(ObjectCategory=user) (userAccountControl=512))", ['sAMAccountName','givenName','sn']
#userAccountControl 512 = normal , 514 = disabled account
for (dn, vals) in res:
accountname = vals['sAMAccountName'].lower()
sirname = vals['sn'].lower()
sirname = vals['sAMAccountName'].lower()
givenname = vals['givenName']
givenname = vals['sAMAccountName'].lower()
initial = givenname[:1].upper()
sirname = sirname.capitalize()
name = initial + "." + sirname
accountname = accountname + "@" + domain
password = " \'\' "
os.system('echo command ca %s %s displayName %s' % (accountname,password,name))
except ldap.LDAPError, error_message:
ps - please feel free to improve/use/laugh at this code