from ldap3 import Server, Connection, SUBTREE
s = Server(ad_domain, use_ssl=True, port=636) # Адрес домена, шифрование, порт
c = Connection(s, ad_admin, ad_password)
# Поиск учётной записи в домене (Active Directory) по фамилии или логину
def search_ad(last='*', login='*'):
c.bind()
c.search('ou=Офис, dc=domain, dc=local',
'(&(objectCategory=Person)(sn={})(sAMAccountName={}))'.format(last, login), SUBTREE,
attributes=['sAMAccountName', 'givenName', 'sn', 'cn', 'whenCreated', 'lastLogon', 'pwdLastSet',
'msDS-UserPasswordExpiryTimeComputed', 'memberOf', 'ipPhone', 'lockoutTime',
'distinguishedName'])
a = c.entries
c.unbind()
return a
# Вывод информации учётной записи в домене (Active Directory)
def change_ad(cn):
c.bind()
c.search('ou=Офис, dc=domain, dc=local', '(&(objectCategory=Person)(cn={}))'.format(cn), SUBTREE,
attributes=['sAMAccountName', 'givenName', 'sn', 'cn', 'whenCreated', 'lastLogon', 'pwdLastSet',
'msDS-UserPasswordExpiryTimeComputed', 'memberOf', 'ipPhone', 'lockoutTime'])
a = c.entries
for ad_user in c.entries:
name = ad_user['givenName']
last = ad_user['sn']
login = ad_user['sAMAccountName']
# Отправка электронного письма
def send_email(message):
msg_email = MIMEMultipart()
msg_email['From'] = email_from
msg_email['To'] = email_to
msg_email['Subject'] = 'Тема'
body = 'Текст сообщения'
msg_email.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP_SSL('smtp.yandex.ru:465')
server.login(email_from, email_password)
text = msg_email.as_string()
server.sendmail(email_from, email_to, text)
server.quit()