#!/usr/bin/python import ldap, string, os, time, sys from creole.client import CreoleClient dico = CreoleClient().get_creole() domain = dico['nom_domaine_local'] ad_server = dico['ad_server'] ad_workgroup = dico['ad_workgroup'] nom_scribe = dico['nom_machine'] hostfilepath = "/home/wpkg/hosts.xml" LDAP_ADDR = ''.join(["ldaps://",str(ad_server),".",str(domain),":636"]) LDAP_BIND = ''.join(["CN=eole-workstation-reader,CN=Users,DC=",str(ad_workgroup),",DC=lan"]) # recuperation du mot de passe de eole-workstation-reader reader = open('/etc/eole/private/eole-workstation-reader.password','r') user_pwd = reader.readline() user_pwd = user_pwd.replace('\n','') # Open the LDAP connection try: # enable tls connexion ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) l = ldap.initialize(LDAP_ADDR) except ldap.LDAPError, e: print e sys.exit(1) # Set protocol version to LDAPv3 l.protocol_version = ldap.VERSION3 # Bind to AD try: l.bind_s(LDAP_BIND,user_pwd) except ldap.LDAPError, e: print e sys.exit(1) scope = ''.join(["OU=Parcs,dc=",ad_workgroup,",dc=lan"]) os.system('/bin/rm -rf %s' % ("/home/wpkg/hosts.xml")) HostFile = open("/home/wpkg/hosts.xml","a+") HostFile.write("\n\n") try: res = l.search_s(scope, ldap.SCOPE_SUBTREE, "(&(ObjectCategory=computer) )", ['name', 'canonicalName']) for (dn, vals) in res: accountname = vals['name'][0].lower() try: ou = vals['canonicalName'][0].lower() except: ou = vals['name'][0].lower() ou = ou.replace(accountname,' ') ou = ou.replace(' ','') ou = ou.replace(''.join([domain,"/parcs/"]),'') ou = ou.replace('/','-') ou = ou.rstrip('-') HostFile.writelines("\n") except ldap.LDAPError, error_message: print error_message scope = ''.join(["CN=Computers,dc=",ad_workgroup,",dc=lan"]) try: res = l.search_s(scope, ldap.SCOPE_SUBTREE, "(&(ObjectCategory=computer) )", ['name', 'canonicalName']) for (dn, vals) in res: accountname = vals['name'][0].lower() try: ou = vals['canonicalName'][0].lower() except: ou = vals['name'][0].lower() if ou == ''.join([domain,"/computers/",nom_scribe]): print("") else: ou = ou.replace(accountname,' ') ou = ou.replace(' ','') ou = ou.replace(''.join([domain,"/"]),'') ou = ou.replace('/','-') ou = ou.rstrip('-') HostFile.writelines("\n") except ldap.LDAPError, error_message: print error_message HostFile.writelines("\n") HostFile.close() l.unbind_s()