Jump to content

Python script ,escaped quotes - zimbra account autocreation


Recommended Posts

Posted

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

zmprov ca username@domain '' displayname name

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:

>>> password = "\'\'"
>>> print password
''
>>>   

but not if I use

 os.system('echo command  %s ' % (password)) 

... I gust get blanks for the password and no quotes, I also tried triplequoting

 

script

#!/usr/bin/python
# 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
l.simple_bind_s("domain\\ldap_username","ldap_password")
try:
 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'][0].lower()
   try:
     sirname = vals['sn'][0].lower()
   except:
     sirname = vals['sAMAccountName'][0].lower()
   try:
     givenname = vals['givenName'][0]
   except:
     givenname = vals['sAMAccountName'][0].lower()

   initial = givenname[:1].upper()
   sirname = sirname.capitalize()
   name = initial + "." + sirname
   accountname = accountname + "@" + domain
   password = "  \'\' "
   print password
   print accountname,name
   os.system('echo command ca %s %s displayName %s' % (accountname,password,name))

except ldap.LDAPError, error_message:
 print error_message

l.unbind_s()

 

Any help appreciated.

 

ps - please feel free to improve/use/laugh at this code

Posted

As the password will be blank regardless why don't you just have the quotes in the string e.g.

os.system('echo command ca %s \'\' displayName %s' % (accountname,name))

 

You could try insrting actual ASCII for single quote as either hex or octal.

Then again are you using UNICODE?

Posted

thanks networkgeezer - I did try escaping them in the string but it seems that I caused the problem using 'echo' to test the script - when I changed it to run zmprov it worked first time. - It was a dumb mistake but I should have realised that issuing echo with

 echo '' 

prints nothing!

 

horray - no more manual email account creation !

Posted
Hmmmm .... this is all a bit fishy. You just wanted to show off your Python code

 

lol, it was an honest mistake. I wanted to be dead sure it was working before I started issuing system commands! besides, I've only ever written a couple of other scripts, so its probably not the most elegant.

 

Why don't you just start a blog on your adventures with Zimbra.

 

Yes. I should. I'm making good progress so far, It would make good documentation for when it all goes titsup :/

  • 3 years later...
Posted

I believe this is the script that I've been using for my Zimbra system. It's worked perfectly in the past, but we have recently moved across to a new domain and so I have been trying to update the script so that it syncronises with the new AD server.

 

However, after changing all of the attributes, when I now try to run the script it returns nothing - no errors or anything, it just starts a new line.

 

I'm very confused - it's as if it's connected to the server (if i change the server or usernames it returns errors as it cannot connect) but is just not finding any accounts?! We've moved to a server 2008 domain if this helps.

 

Any help would be much appreciated! Below is a copy of the attributes I have changed..

 

 

# Variables can be changed here:

banned = 'CN=Restricted Users,OU=Logon Groups,OU=Security Groups,OU=Establishment,DC=HPHS,DC=INTERNAL'

# an OU for banned users

scope = 'OU=Establishment,DC=HPHS,DC=INTERNAL'

#the search scope

domain = "HPHS.internal" # "example.com"

ldapserver="hps-dc-01"

#ldap server

port="389"

#ldap port (389 default)

emaildomain="xxx.leics.sch.uk"

#the email domain

ldapbinddomain="HPHS"

#the domain of the ldap bind account

ldapbind="xxx"

#the account name of the account to bind to ldap

ldappassword="xxx"

#the ldap password

pathtozmprov="/opt/zimbra/bin/zmprov"

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...