Skip to content

Instantly share code, notes, and snippets.

@Vankalif
Created June 4, 2020 18:29
Show Gist options
  • Save Vankalif/337564b21aa28eadd8c1956de345ae98 to your computer and use it in GitHub Desktop.
Save Vankalif/337564b21aa28eadd8c1956de345ae98 to your computer and use it in GitHub Desktop.
from ldap3 import Server, Connection, ALL
# OU которые в которых не нужно искать, регистрозависимые
trash = ['system',
'Computers',
'Временное',
'KMKR',
'turned_off']
ous = []
server = Server('172.16.0.3', get_info=ALL)
conn = Connection(
server,
user='ползователь@KMKR.LOCAL',
password='пароль',
auto_bind=True
)
conn.search('OU=KMKR,DC=kmkr,DC=local',
search_filter='(ObjectClass=organizationalunit)',
attributes=['ou'])
for entry in conn.entries:
if entry.ou.value not in trash:
ous.append(entry.ou.value)
for ou in ous:
unit = ou.split(' ')[0]
with open(f'/var/www/html/book/{unit}.xml', 'w', encoding='utf-8') as file:
file.write('<KMKRIPPhoneDirectory>\n')
conn.search(f'OU={ou},OU=KMKR,DC=kmkr,DC=local',
search_filter='(ObjectClass=person)',
attributes=['cn', 'ipPhone'])
for user in conn.entries:
if user.ipPhone.value is not None:
file.write(f'\t<DirectoryEntry>\n\t\t<Name>{user.cn.value}</Name>\n\t\t<Telephone>{user.ipPhone.value}</Telephone>\n\t</DirectoryEntry>\n')
file.write('</KMKRIPPhoneDirectory>')
conn.unbind()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment