Python

28 juin 2021 Non Par nospheratus

LDAP :

installation :

python3 -m pip install python-ldap

Connexion

from ldap3 import Server, Connection, ALL, MODIFY_REPLACE
s = Server(« refid-ldap-ecriture.nosland.com », port=636, use_ssl = True , get_info=ALL)
c = Connection(s, user= »cn=Admin,dc=nosland,dc=com », password= »MonMotDePass », check_names=True, lazy=False, raise_exceptions=True)
c.bind()
c.open()

Requête

Rechercher un enregistrement

c.search(search_base = ‘dc=nosland,dc=com’, search_filter = « (&(objectClass=supannPerson)(uid= » + uidetrouver + « )) », attributes = [‘uid’,’o’,’ou’,’fdBadge’])

Recherche avec boucle sur tous les enregistrements et affiche deux champs

c.search(search_base = ‘dc=nosland,dc=com’,
search_filter = ‘(objectclass=supannPerson)’,
attributes = [‘uid’,’givenName’,’sn’,’o’,’ou’,’modifyTimestamp’,’employeeNumber’,’telephoneNumber’])
for entry in c.entries:
print(str(entry.uid) +  »  » + str(entry.givenName))

Modification :

c.modify(‘uid=’+ qui + ‘,ou=people,dc=nosland,dc=com’, {‘giveName’: [(MODIFY_REPLACE, [‘NosPHeratus’])]} )

Commande Linux en python

import os
os.system(‘cat /opt/echanges/PourDSI/export.csv |grep -v inactif > badgesactifs.csv’)
os.system(‘sed -e « s/,Profil actif// » badgesactifs.csv > badgesactif.csv’)

Commandes Python (sialorssinon, essaye, boucle)

Essaye et si echoue : try/except :

try :
commandes 1 à faire
commandes 2 à faire
etc….
except:
commandes 1 à faire
commandes 2 à faire
etc….

Si Alors Sinon :

if (conditions):
commandes 1 à faire
commandes 2 à faire
etc….
else:
commandes 1 à faire
commandes 2 à faire
etc….

Boucle for :

for condition:
commandes 1 à faire
commandes 2 à faire
etc….
#Fin de boucle avec arrêt de la tabulation