zoukankan      html  css  js  c++  java
  • zabbix 同步ldap帐号脚本

    1、界面配置ldap验证(略)

    2、mysql导入ldap帐号信息

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-

    import pymysql
    import commands
    import base64
    import sys
    from datetime import datetime

    reload(sys)
    sys.setdefaultencoding('utf-8')

    mysql_host='xxx.internal.xxx.com'
    mysql_port=3306
    mysql_user='zabbix'
    mysql_passwd='zabbix'
    mysql_db='zabbix'

    def get_user_from_ldap ():
    ldap_users=commands.getoutput("ldapsearch -x -LLL -H ldap://xxx.xxx.xxx.xxx-b dc=xx,dc=com givenName|sed '1,12'd|sed '/^$/d'|egrep -v 'ou=Group|ou=machines'").strip().splitlines()
    return ldap_users


    def get_value():
    values = {}
    lines = get_user_from_ldap()
    for index,line in enumerate(lines):
    if line.startswith("dn: cn="):
    if (index+1)<len(lines) and lines[index+1].startswith("givenName:"):
    strcount = lines[index+1].strip().split()[0].count(':')
    if strcount == 2:
    sourcename = lines[index+1].strip().split()[1]
    name = base64.b64decode(sourcename)
    else:
    name = lines[index+1].strip().split()[1]
    alias = lines[index].strip().split('=')[1].split(',')[0]
    values[alias] = name
    else:
    print "The user [%s] set error on ldap server !" % line
    return values


    def get_id():
    conn = pymysql.connect(host=mysql_host, port=mysql_port, user=mysql_user, passwd=mysql_passwd, db=mysql_db, charset='utf8')
    cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cur.execute("select userid from users order by userid desc limit 1")
    id = cur.fetchone().values()[0]
    cur.close()
    conn.close()
    return id

    def get_mysql_data():
    conn = pymysql.connect(host=mysql_host, port=mysql_port, user=mysql_user, passwd=mysql_passwd, db=mysql_db, charset='utf8')
    cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cur.execute("select userid from users order by userid desc limit 1")
    id = cur.fetchone().values()[0]
    cur.close()
    cur_user = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cur_user.execute("select alias from users")
    user_data = cur_user.fetchall()
    user_name = [name['alias'] for name in user_data]
    cur_user.close()
    conn.close()
    return id,user_name


    def insert_data():
    conn = pymysql.connect(host=mysql_host, port=mysql_port, user=mysql_user, passwd=mysql_passwd, db=mysql_db, charset='utf8')
    cur = conn.cursor()
    n, zabbix_user = get_mysql_data()
    # del_name = []
    data = get_value()
    ldap_name = data.keys()
    for alias, name in data.items():
    search = cur.execute("select * from users where alias = %s", (alias, ))
    if not search:
    n += 1
    sql = "insert into users(userid,name,alias) values ('%s','%s','%s');" % (n,name,alias)
    insert = cur.execute(sql)
    if insert:
    with open ('/opt/ldap.log', 'a') as log:
    time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    str = "User %s Name's %s and Userid's %s Add Succeed at %s" % (alias,name,n,time)
    log.write(str+' ')
    del_cur = conn.cursor()
    for name in zabbix_user:
    if name not in ldap_name:
    if name.lower() != 'zabbix' or name.lower() != 'guest':
    # del_name.append(name)
    try:
    del_data = del_cur.execute("delete from users where alias = %s", (name,))
    if del_data:
    with open ('/opt/ldap.log', 'a') as del_log:
    del_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    del_str = "User %s del succeed at %s" % (name, del_time)
    del_log.write(del_str+' ')
    except (Exception), error_str:
    print error_str
    print "when del %s failed" % name
    # print del_name
    conn.commit()
    cur.close()
    conn.close()

    if __name__ == '__main__':
    insert_data()

  • 相关阅读:
    生成流畅文本方法
    KNNImputer:一种可靠的缺失值插补方法
    SparseNN中的优化
    基于TorchText的PyTorch文本分类
    蒙特卡洛方法分析Web页面浏览量
    D. Segment Intersections 模拟
    B. Array Walk
    线性基 P3857 [TJOI2008]彩灯
    线性基 P4570 [BJWC2011]元素
    线性基的学习+总结
  • 原文地址:https://www.cnblogs.com/Qing-840/p/9267910.html
Copyright © 2011-2022 走看看