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.

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

Powered by TurnKey Linux.