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.

94 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
6 years ago
6 years ago
6 years ago
6 years ago
4 years ago
4 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.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. enabled = False
  37. pwchange = False
  38. toremove = False
  39. # преобразуем флаги в удобную форму
  40. if i['Activ'] == 'Y':
  41. enabled = True
  42. if i['Changes'] == 'Y':
  43. pwchange = True
  44. if i['Delete'] == 'Y':
  45. toremove = True
  46. # размечаем группы, в которых должен быть пользователь
  47. memberof = []
  48. if str(i['Otdel']) == 'Users':
  49. memberof = ad_groups_users
  50. elif str(i['Otdel']) == '1C':
  51. memberof = ad_groups_1c
  52. elif str(i['Otdel']) == 'Bitrix':
  53. memberof = ad_groups_bitrix
  54. if str(i['XServ']) == '1':
  55. print(i)
  56. memberof.append(ad_group_xserv)
  57. if str(i['RDP']) == '1':
  58. print(i)
  59. memberof.append(ad_group_rdp)
  60. # собственно добавляем в мапу
  61. res[usrname] = {'id': str(i['ID']),
  62. 'samAccountName': usrname,
  63. 'memberOf': memberof,
  64. 'usrPass': str(i['Pass']),
  65. 'groupSet': str(i['Otdel']),
  66. 'enabled': enabled,
  67. 'pwChange': pwchange,
  68. 'toRemove': toremove,
  69. 'email': i['LoginEmail']
  70. }
  71. return res
  72. def main():
  73. data, err = mysql_reader(dbconn, sql)
  74. if not err:
  75. users = users_make_zayavka(data)
  76. for user in users:
  77. if '1' in users[user]['groupSet']:
  78. print user, users[user]
  79. else:
  80. print err
  81. if __name__ == '__main__':
  82. main()

Powered by TurnKey Linux.