Project

General

Profile

gen-wpkg-fichier-hosts1.py

generation de hosts.xml avec des profile-id parcs-%nom% - David PAILLER, 12/17/2019 02:39 PM

Download (2.89 KB)

 
1
#!/usr/bin/python
2
import ldap, string, os, time, sys
3
from creole.client import CreoleClient
4

    
5
dico = CreoleClient().get_creole()
6
domain = dico['nom_domaine_local']
7
ad_server = dico['ad_server']
8
ad_workgroup = dico['ad_workgroup']
9
nom_scribe = dico['nom_machine']
10
hostfilepath = "/home/wpkg/hosts.xml"             
11
LDAP_ADDR = ''.join(["ldaps://",str(ad_server),".",str(domain),":636"])
12

    
13
LDAP_BIND = ''.join(["CN=eole-workstation-reader,CN=Users,DC=",str(ad_workgroup),",DC=lan"])
14
#  recuperation du mot de passe de eole-workstation-reader
15
reader = open('/etc/eole/private/eole-workstation-reader.password','r')
16
user_pwd = reader.readline()
17
user_pwd = user_pwd.replace('\n','')
18

    
19
# Open the LDAP connection
20

    
21
try:
22
    # enable tls connexion
23
   
24
    ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
25
    
26
    l = ldap.initialize(LDAP_ADDR)
27
    
28
    
29
except ldap.LDAPError, e:
30
    print e
31
    sys.exit(1)
32
 
33
# Set protocol version to LDAPv3
34
l.protocol_version = ldap.VERSION3
35
 
36
# Bind to AD
37
try:
38
    l.bind_s(LDAP_BIND,user_pwd)
39
except ldap.LDAPError, e:
40
    print e
41
    sys.exit(1)
42

    
43
scope = ''.join(["OU=Parcs,dc=",ad_workgroup,",dc=lan"])
44
    
45
os.system('/bin/rm -rf %s'  % ("/home/wpkg/hosts.xml"))
46

    
47
HostFile = open("/home/wpkg/hosts.xml","a+")
48

    
49
HostFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<wpkg>\n")
50

    
51
try:
52
    res = l.search_s(scope, ldap.SCOPE_SUBTREE, "(&(ObjectCategory=computer) )", ['name', 'canonicalName'])
53

    
54
    for (dn, vals) in res:
55
        accountname = vals['name'][0].lower()
56
        try:
57
            ou = vals['canonicalName'][0].lower()
58
        except: 
59
            ou = vals['name'][0].lower()
60

    
61

    
62
        ou = ou.replace(accountname,' ')
63
        ou = ou.replace(' ','') 
64
        ou = ou.replace(''.join([domain,"/"]),'')
65
        ou = ou.replace('/','-')
66
        ou = ou.rstrip('-') 
67

    
68

    
69
        HostFile.writelines("<host name=" + '"' + accountname + '"' +   " profile-id=" + '"' +  ou + '"' + " />\n")
70
      
71
except ldap.LDAPError, error_message:
72
    print error_message          
73

    
74
scope = ''.join(["CN=Computers,dc=",ad_workgroup,",dc=lan"])
75
try:
76
    res = l.search_s(scope, ldap.SCOPE_SUBTREE, "(&(ObjectCategory=computer) )", ['name', 'canonicalName'])
77

    
78
    for (dn, vals) in res:
79
        accountname = vals['name'][0].lower()
80
        try:
81
            ou = vals['canonicalName'][0].lower()
82
        except: 
83
            ou = vals['name'][0].lower()
84

    
85
        if ou == ''.join([domain,"/computers/",nom_scribe]):
86
            print("")
87
        else:
88
            ou = ou.replace(accountname,' ')
89
            ou = ou.replace(' ','') 
90
            ou = ou.replace(''.join([domain,"/"]),'')
91
            ou = ou.replace('/','-')
92
            ou = ou.rstrip('-') 
93
            HostFile.writelines("<host name=" + '"' + accountname + '"' +   " profile-id=" + '"' +  ou + '"' + " />\n")
94

    
95

    
96
except ldap.LDAPError, error_message:
97
    print error_message          
98

    
99
HostFile.writelines("</wpkg>\n")
100
HostFile.close()
101

    
102
l.unbind_s()