附一个验证ldap的python代码。
import socket from ldap3 import Server, Connection, ALLdef is_port_open(host, port):"""检查远程主机的指定端口是否开放:param host: 远程主机地址:param port: 要检查的端口号:return: 端口开放返回True,否则返回False"""try:with socket.create_connection((host, port), timeout=5) as sock:return Trueexcept (socket.timeout, ConnectionRefusedError):return Falsedef test_ldap_service(host, port, user, password):"""使用ldap3库验证LDAP服务是否正常:param host: LDAP服务器地址:param port: LDAP服务器端口:param user: 用户名:param password: 密码:return: 验证成功返回True,否则返回False"""try:server = Server(host, port=port, get_info=ALL)conn = Connection(server, user=user, password=password, auto_bind=False)# 尝试手动绑定if conn.bind():print("LDAP服务验证成功")return Trueelse:print(f"LDAP服务验证失败: {conn.result}")return Falseexcept Exception as e:print(f"LDAP服务验证失败: {e}")return Falseif __name__ == "__main__":ldap_host = "192.168.2.51"ldap_port = 389ldap_user = "cn=odooadmin,ou=LdapUsers,dc=wywr,dc=top"ldap_password = "***"# ldap_user = "cn=administrator,cn=users,dc=wywr,dc=top"# ldap_password = "***"# 检查端口是否开放if is_port_open(ldap_host, ldap_port):print(f"LDAP端口 {ldap_port} 开放")# 验证LDAP服务if test_ldap_service(ldap_host, ldap_port, ldap_user, ldap_password):print("LDAP服务正常")else:print("LDAP服务异常")else:print(f"LDAP端口 {ldap_port} 未开放")
注意:
公司是选的,不是建的
模板用户也是选的,如果没有模板用户,要新建。