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.

96 lines
2.8 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
4 years ago
6 years ago
4 years ago
6 years ago
6 years ago
4 years ago
4 years ago
6 years ago
4 years ago
4 years ago
4 years ago
6 years ago
6 years ago
4 years ago
6 years ago
4 years ago
4 years ago
6 years ago
  1. #!/usr/bin/python2
  2. # coding=utf-8
  3. import MySQLdb
  4. dbconn = {'host': '10.3.11.177',
  5. 'user': 'readonlyuser',
  6. 'pass': 'Readonly@2006',
  7. 'base': 'techgrow'}
  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', 'Domain Users']
  12. ad_group_xserv = 'ics'
  13. ad_group_rdp = 'Пользователи удаленного рабочего стола'
  14. def mysql_reader(dbconn, sql):
  15. data = tuple
  16. err = False
  17. try:
  18. conn = MySQLdb.connect(host=dbconn['host'], user=dbconn['user'],
  19. passwd=dbconn['pass'], db=dbconn['base'], charset="utf8")
  20. cur = conn.cursor(MySQLdb.cursors.DictCursor)
  21. cur.execute(sql)
  22. data = cur.fetchall()
  23. cur.close()
  24. except MySQLdb.Error as exc:
  25. print("Connection error: {}".format(err))
  26. conn.close()
  27. err = exc
  28. else:
  29. conn.commit()
  30. conn.close()
  31. return data, err
  32. def users_make_zayavka(users):
  33. res = {}
  34. for i in users:
  35. usrname = str(i['LoginEmail']).split('@')[0]
  36. if usrname in res:
  37. continue
  38. enabled = False
  39. pwchange = False
  40. toremove = False
  41. res[usrname] = {}
  42. # преобразуем флаги в удобную форму
  43. if i['Activ'] == 'Y':
  44. enabled = True
  45. if i['Changes'] == 'Y':
  46. pwchange = True
  47. if i['Delete'] == 'Y':
  48. toremove = True
  49. # размечаем группы, в которых должен быть пользователь
  50. memberof = []
  51. print(memberof)
  52. if str(i['Otdel']) == 'Users':
  53. memberof = ad_groups_users[:]
  54. elif str(i['Otdel']) == '1C':
  55. memberof = ad_groups_1c[:]
  56. elif str(i['Otdel']) == 'Bitrix':
  57. memberof = ad_groups_bitrix[:]
  58. if str(i['XServ']) == '1':
  59. memberof.append(ad_group_xserv)
  60. if str(i['RDP']) == '1':
  61. memberof.append(ad_group_rdp)
  62. # собственно добавляем в мапу
  63. res[usrname] = {'id': str(i['ID']),
  64. 'samAccountName': usrname,
  65. 'memberOf': memberof,
  66. 'usrPass': str(i['Pass']),
  67. 'groupSet': str(i['Otdel']),
  68. 'enabled': enabled,
  69. 'pwChange': pwchange,
  70. 'toRemove': toremove,
  71. 'email': i['LoginEmail']
  72. }
  73. return res
  74. def main():
  75. data, err = mysql_reader(dbconn, sql)
  76. if not err:
  77. users = users_make_zayavka(data)
  78. for user in users:
  79. print(user, users[user])
  80. else:
  81. print(err)
  82. if __name__ == '__main__':
  83. main()

Powered by TurnKey Linux.