+ Post New Thread
Results 1 to 6 of 6
Scripts Thread, Zimbra Active Directory Sync in Coding and Web Development; This is a script written in python that will add user accounts in AD into Zimbra. It uses the LDAP ...
  1. #1

    Join Date
    Sep 2009
    Location
    West Midlands
    Posts
    18
    Thank Post
    7
    Thanked 3 Times in 2 Posts
    Rep Power
    11

    Zimbra Active Directory Sync

    This is a script written in python that will add user accounts in AD into Zimbra. It uses the LDAP module for python so this is needed before you run the script. This script allows you to define the class of service you wish to use.

    If you have any questions I will do my best to answer them.


    Code:
    #!/usr/bin/python
    
    import ldap,sys,os,time
    
    baseDN = "ou=, dc=example, dc=com"
    filter = "sn=*"
    pathtozmprov="/opt/zimbra/bin/zmprov"
    f = os.popen(pathtozmprov +' -l gaa ')
    zmprovgaa = []
    zmprovgaa = f.readlines()
    
    try:    	
    	l = ldap.initialize("path to ldap server") 
        	l.simple_bind_s("cn=user, dc=example,dc=com","password") #User with rights to bind to LDAP
       	print "Successfully bound to server.\n"
        	
    
      	result = l.search_s(baseDN, ldap.SCOPE_SUBTREE,filter)
    
        	for (dn, vals) in result:
          
    		accountname = vals['sAMAccountName'][0].lower()
          		mail = vals['mail'][0]
    		disN = vals['displayName'][0]
    		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.replace(' ', '')
    		sirname = sirname.replace('\'', '')
    		sirname = sirname.replace('-', '')
    		sirname = sirname.capitalize()
    		name = "'" + givenname + " " + sirname +"'" 
          		accountname = accountname + "@" + domain
    		password = "  \'\' "
    
                    sys.stdout.flush()
        
                    if mail +"\n" not in zmprovgaa:  
                    print accountname," exists in active directory but not in zimbra, the account is being                    created\n"
            
    		time.sleep(1)
    	
    		os.system(pathtozmprov + ' ca %s %s displayName %s givenName %s sn %s ' % (mail,password,name,givenname,sirname))
                    print "The following account was added " + mail + "\n"
    	        cos = raw_input ('Which Class of Service? ')
                    os.system(pathtozmprov + ' sac %s %s ' % (accountname,cos))
    	
    except ldap.LDAPError, error_message:
    print error_message         
    l.unbind_s()
    Last edited by Vortex566; 15th January 2010 at 11:44 AM.

  2. 2 Thanks to Vortex566:

    dhicks (15th January 2010), Hightower (15th January 2010)

  3. #2

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    242
    I get error on line 46: expected indented block

  4. #3

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    242
    Sorted

  5. #4

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    242
    Quote Originally Posted by Vortex566 View Post
    This is a script written in python that will add user accounts in AD into Zimbra. It uses the LDAP module for python so this is needed before you run the script. This script allows you to define the class of service you wish to use.
    Thanks for this! Such a big help

    I've tweaked it to suit a little. First I've made a script that adds all my student accounts to the student COS (based on they all begin with two numbers). I have that running now.

    Then I have another script which actually asks "Do you want to create this account [y/n]" as we have some accounts in AD that are exam/temp accounts which we do not want mailboxes setting up for.

    Once again, thanks!

  6. #5

    Join Date
    Sep 2009
    Location
    West Midlands
    Posts
    18
    Thank Post
    7
    Thanked 3 Times in 2 Posts
    Rep Power
    11
    No problem glad it helped... I myself based it on a script I found on here. I had to mod it a lot to get it to work but I got there in the end.

    I hope to add more features when I get the time.

    Once again I am glad it helped.

  7. #6

    Join Date
    Jan 2012
    Location
    Los Angeles, CA
    Posts
    1
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Hightower, would you mind posting your script? I also am in need of something similar.



SHARE:
+ Post New Thread

Similar Threads

  1. Open Directory Sync with AD
    By binky in forum General Chat
    Replies: 0
    Last Post: 5th June 2009, 09:48 PM
  2. Replies: 0
    Last Post: 6th April 2009, 11:26 PM
  3. Zimbra active directory intergration
    By reggiep in forum *nix
    Replies: 0
    Last Post: 6th January 2009, 10:09 AM
  4. MS Active Sync for Vista
    By sLiDeR in forum Windows
    Replies: 3
    Last Post: 25th March 2008, 06:56 PM
  5. Replies: 7
    Last Post: 31st January 2008, 01:17 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •