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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值