You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. #!/usr/bin/python2
  2. # coding=utf-8
  3. import MySQLdb
  4. dbconn = {'host': '10.1.11.26',
  5. 'user': 'LDAP2018',
  6. 'pass': 'Pr4t7UEZITwW1AYG',
  7. 'base': 'sitemanager0'}
  8. sql = 'select * from LDAP'
  9. ad_groups_users = ['vpn-users', 'gogs']
  10. ad_groups_bitrix = ['vpn-web-vpn', 'gogs']
  11. ad_groups_1c = ['vpn-1c-vpn']
  12. def mysql_reader(dbconn, sql):
  13. data = tuple
  14. err = False
  15. try:
  16. conn = MySQLdb.connect(host=dbconn['host'], user=dbconn['user'],
  17. passwd=dbconn['pass'], db=dbconn['base'], charset="utf8")
  18. cur = conn.cursor(MySQLdb.cursors.DictCursor)
  19. cur.execute(sql)
  20. data = cur.fetchall()
  21. cur.close()
  22. except MySQLdb.Error as exc:
  23. print("Connection error: {}".format(err))
  24. conn.close()
  25. err = exc
  26. else:
  27. conn.commit()
  28. conn.close()
  29. return data, err
  30. def users_make_zayavka(users):
  31. res = {}
  32. for i in users:
  33. usrname = str(i['LoginEmail']).split('@')[0]
  34. enabled = False
  35. pwchange = False
  36. toremove = False
  37. # преобразуем флаги в удобную форму
  38. if i['Activ'] == 'Y':
  39. enabled = True
  40. if i['Changes'] == 'Y':
  41. pwchange = True
  42. if i['Delete'] == 'Y':
  43. toremove = True
  44. # размечаем группы, в которых должен быть пользователь
  45. if str(i['Otdel']) == 'Users':
  46. memberof = ad_groups_users
  47. elif str(i['Otdel']) == '1c':
  48. memberof = ad_groups_1c
  49. elif str(i['Otdel']) == 'Bitrix':
  50. memberof = ad_groups_bitrix
  51. # собственно добавляем в мапу
  52. res[usrname] = {'id': str(i['ID']),
  53. 'samAccountName': usrname,
  54. 'memberOf': memberof,
  55. 'usrPass': str(i['Pass']),
  56. 'groupSet': str(i['Otdel']),
  57. 'enabled': enabled,
  58. 'pwChange': pwchange,
  59. 'toRemove': toremove
  60. }
  61. return res
  62. def main():
  63. data, err = mysql_reader(dbconn, sql)
  64. if not err:
  65. users = users_make_zayavka(data)
  66. for user in users:
  67. print user, users[user]
  68. else:
  69. print err
  70. if __name__ == '__main__':
  71. main()

Powered by TurnKey Linux.