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 ...
随机推荐
- Django:RestFramework之-------频率控制
5.频率控制 5.1频率监控代码实现 import time VISIT_RECORD = {} class VisitThrottle(object): def __init__(self): #定 ...
- Java JDBC事务
JDBC默认是自动提交,事务是关闭的,statement|preparedStatement.executeUpdate()或excute()执行增删改,执行一次就提交一次(自动同步到数据库). JD ...
- 【Python】生成器
生成器是一种特殊的迭代器 # 斐波那契数列 10 def create_num(all_num): a, b = 0, 1 current_num = 0 while current_num < ...
- Linux开发环境配置大全
Linux开发环境配置 零章:通过xshell在linux上安装JDK8 壹章:通过xshell在linux上安装tomcat8 贰章:通过xshell在linux上安装mysql5.7(终极版) 叁 ...
- 用Nodejs遍历云存储文件
起因 最近想要将云存储中的文件去重.因为有现成的Nodejs的API,所以打算用Nodejs实现此功能. 伪代码如下: scanDir = function(uri){ return new Prom ...
- Gzip模块
Gzip模块为python的压缩和解压缩模块,读写gzip 文件 一.使用gzip模块压缩文件: 1 import gzip #导入python gzip模块,注意名字为全小写 2 g = gzip. ...
- Httpd服务进阶知识-基于FASTCGI实现的LAMP架构
Httpd服务进阶知识-基于FASTCGI实现的LAMP架构 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.httpd+php结合的方式 module: php fastcgi ...
- php单例型(singleton pattern)
搞定,吃饭 <?php /* The purpose of singleton pattern is to restrict instantiation of class to a single ...
- selenium-1-python
python selenium from selenium import webdriver from selenium.webdriver.common.action_chains import A ...
- 项目Beta冲刺(团队)——博客集合
项目Beta冲刺(团队)--博客集合 格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Beta冲刺(团队) 团队名称:为了交项目干杯 作业目标:集中记录所有Beta敏捷冲刺日志的集 ...