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+'\n')
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+'\n')
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()
zabbix 同步ldap帐号脚本的更多相关文章
- VBS处理AD帐号密码到期提醒的脚本[zt]
原文:https://gallery.technet.microsoft.com/scriptcenter/f7f5f7ed-14ee-4d0e-81c2-7d95ce7e08f5 '======== ...
- shell脚本,批量创建10个系统帐号并设置密码为随机8位字符串。
[root@localhost wyb]# cat user10.sh #!/bin/bash #批量创建10个系统帐号wangyb01-wangyb10并设置密码(密码为随机8位字符串). > ...
- 如何修改SharePoint2013服务器场帐号密码
服务器远程登录帐号密码修改密码后,如何修改sharepoint服务器场管理员账户密码,今天登录了一下N久以前的搭建sharepoint2013服务器场的一台服务器器,登录进去以后直接提示帐号密码过期需 ...
- wex5 实战 登陆帐号更换与用户id一致性
一 前言 关于用户登陆,注册,页面跳转,我讲了很多,都是页面框架级别的.有人问到这个问题,索性总结一下,今天是收尾最后一篇. 以往初学时的设计是: 1 注册 2 登陆 3 进入页面. 这种很简单,没有 ...
- 在SQL Server中添加供应用程序使用的帐号
在之前客户咨询案例中,很多客户应用程序连接SQL Server直接用的就是SA帐号.如果对数据库管理稍微严格一点的话,就不应该给应用程序这种权限,通常应用程序只需要进行增删改查,而很少有DDL ...
- 3种用组策略将域帐号加入本地管理员组的方法_jinifly_新浪博客
次当前系统域帐号是怎么在第一次登录时,自动加入域客户端本地管理员组的?我猜不外乎就是脚本.计算机策略或虚拟机初始化的自动应答脚本,结果系统的前任同事找到了答案--GPO的用户策略(确切讲是用户首选项) ...
- 为github帐号添加SSH keys
为github帐号添加SSH keys 2012-05-26 00:05 34279人阅读 评论(6) 收藏 举报 ssh文本编辑gitvim工具up 使用git clone命令从github上同步g ...
- 用firefox 31配合KeePass密码管理器实现web帐号密码自动填写登录
原文:http://bbs.kafan.cn/thread-1754676-1-1.html KeePass的优势:1.这是一款完全开源的密码管理器2.很多人都使用lastpass来保存密码,而这种严 ...
- Chapter 2 User Authentication, Authorization, and Security(2):创建登录帐号
原文出处:http://blog.csdn.net/dba_huangzj/article/details/38705965,专题目录:http://blog.csdn.net/dba_huangzj ...
随机推荐
- 2019 阿里java面试笔试总结 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.滴滴等公司offer,岗位是Java后端开发,因为发展原因最终选择去了阿里,入职一年时间了,也成为了面试官, ...
- Java自学-类和对象 枚举类型
枚举类型 步骤 1 : 预先定义的常量 枚举enum是一种特殊的类(还是类),使用枚举可以很方便的定义常量 比如设计一个枚举类型 季节,里面有4种常量 public enum Season { SPR ...
- 【转载】C#通过InsertAt方法在DataTable特定位置插入一条数据
在C#中的Datatable数据变量的操作过程中,可以通过DataTable变量的Rows属性的InsertAt方法往DataTable的指定位置行数位置插入一个新行数据,即往DataTable表格指 ...
- js数组与字符串类型相同方法的比较
数组和字符串有很多相似的对方,比如数组和字符串都有以下方法: concat indexOf lastIndexOf slice includes 鉴于toString及valueOf方法基本类型都有, ...
- JavaScript 之 基本包装类型
基本包装类型 为了方便操作基本数据类型,JavaScript 还提供了三个特殊的引用类型:String/Number/Boolean. 下面先看一段代码: var s1 = "Hello ...
- 解决bootstrap模态框居中问题
完美解决办法: 在bootstrap.js或bootstrap.min.js文件中找到Modal.prototype.show方法. 在that.$element.addClass('in').att ...
- LP线性规划求解 之 单纯形 算法
LP线性规划求解 之 单纯形 算法 认识-单纯形 核心: 顶点旋转 随机找到一个初始的基本可行解 不断沿着可行域旋转(pivot) 重复2,直到结果不能改进为止 案例-过程 以上篇的case2的松弛型 ...
- 关于logging模块
from logging.handlers import TimedRotatingFileHandle #日志文件控制(日志删除时间设置) import logging logger=logging ...
- MongoDB配置文件及添加用户
一.参数启动mongodb $ mongod --bind_ip=0.0.0.0 二.配置文件: mongod.conf # 后台运行 fork=true # 数据存储文件目录 dbpath=/roo ...
- Linux 环境变量配置错误,导致所有命令找不到
今天配置环境变量,PATH设置出错,所有的命令都找不到了,提示说在/usr/bin/下面可以找到,但是cd过去以后还是不行,自己也在其他路径上找了,还是没找到 而且是公司开发机,怕耽误事儿,着实吓了一 ...