Python 尝试在LDAP中搜索2个文件夹中的用户
如果我选择“和”,它将只搜索第二个文件夹Python 尝试在LDAP中搜索2个文件夹中的用户,python,function,ldap,conditional-statements,python-ldap,Python,Function,Ldap,Conditional Statements,Python Ldap,如果我选择“和”,它将只搜索第二个文件夹 #Users Accounts path bind_dn = "cn="+user+",ou=" +user[0]+",OU=Useraccounts,OU=Mtp,DC=us,DC=bosch,DC=com" and "cn="+user+",ou=" +user[0]+",OU=Useraccounts,OU=Ca1,DC=br,DC=bos
#Users Accounts path
bind_dn = "cn="+user+",ou=" +user[0]+",OU=Useraccounts,OU=Mtp,DC=us,DC=bosch,DC=com" and "cn="+user+",ou=" +user[0]+",OU=Useraccounts,OU=Ca1,DC=br,DC=bosch,DC=com"
bind_pass = password
当我执行“或”时,它只搜索第一个文件夹
这是用于LDAP身份验证的,它将在这些文件夹中搜索用户名以确保它们存在。是否有一个函数可用于让此绑定搜索两个文件夹中的用户,而不是仅搜索其中一个文件夹
用于连接LDAP的函数
def connect_ldap(用户、密码):
bind\u dn
是一个字符串,因此它将右侧的所有内容转换为单个字符串。使用和
或或
的区别在于这些关键字对字符串的操作方式
#CHANGE TO YOUR LDAP SERVER HERE
#LDAP Server
ldap_server = "bosch.com"
#CHANGE TO YOUR BIND_DN PATH HERE
#Users Accounts path
bind_dn = "cn="+user+",ou=" +user[0]+",OU=Useraccounts,OU=Mtp,DC=us,DC=bosch,DC=com" and "cn="+user+",ou=" +user[0]+",OU=Useraccounts,OU=Ca1,DC=br,DC=bosch,DC=com"
bind_pass = password
#Config the server and connection
server = Server(ldap_server, port=int(636), use_ssl=bool(True))
conn = Connection(server=server, user=bind_dn, password=bind_pass)
#First make a touchbase in the LDAP Server with the credentials to authenticate
connection_status = conn.bind()
print("Status: ",connection_status)
# If the user and pass is correct it will continue the script
if connection_status == True:
#Filter the search to Groups
search_filter = '(objectClass=group)'
try:
#CHANGE TO YOUR GROUP SEARCH HERE
#This search will return a members list of the selected group
conn.search("CN=CI/OSR-NA Staff,OU=Recipients,OU=MAIL34,OU=DL,OU=MSX,DC=us,DC=bosch,DC=com",
search_filter, search_scope=SUBTREE, attributes=['member'])
members = []
#Set the list in a variable
for entry in conn.entries:
members = entry.member.values
print("\nGroup Members: \n\n", members, "\n")
status = "Permission Denied"
#Check if the user is part of the group
for member in members:
#If the user is part of the group it will return "Permission Allowed" and terminate the script.
if user.lower() in member.lower() or user.upper() in member.upper():
status = "Permission Allowed"
return status
#If the user is not part of the group it will return "Permission Denied" and terminate the script.
if status == "Permission Denied":
return status
except Exception as e:
return e
# If the user and pass is incorrect it will return "False" and terminate the script.
elif connection_status == False:
return "Connection error"
表示:如果字符串a
有值,则使用字符串b
。在您的例子中,因为第一个字符串是常量字符串,所以它总是有一个值,所以结果总是第二个字符串
a and b
意思是:如果字符串a
有值,则使用该值。否则,请使用字符串b
。在您的例子中,因为第一个字符串是常量字符串,所以它总是有一个值,以便始终使用第一个字符串
这种语法对于静态字符串毫无意义,因为结果总是相同的。您通常将此语法用于变量。你可以通过阅读来了解这些非常有用的案例
您需要将其设置为不带和或或的单个字符串。您还没有显示使用这些变量的代码,我也无法从下面的代码中找到它。但是如果您使用base\u dn
进行身份验证,那么您所做的将取决于LDAP服务器正在运行的内容。如果您使用的是Active Directory,我知道它只接受用户名-您不需要完整的DN。因此,如果您正在使用AD,您可以这样做:
a or b
bind\u dn
是一个字符串,因此它将右侧的所有内容转换为单个字符串。使用和
或或
的区别在于这些关键字对字符串的操作方式
#CHANGE TO YOUR LDAP SERVER HERE
#LDAP Server
ldap_server = "bosch.com"
#CHANGE TO YOUR BIND_DN PATH HERE
#Users Accounts path
bind_dn = "cn="+user+",ou=" +user[0]+",OU=Useraccounts,OU=Mtp,DC=us,DC=bosch,DC=com" and "cn="+user+",ou=" +user[0]+",OU=Useraccounts,OU=Ca1,DC=br,DC=bosch,DC=com"
bind_pass = password
#Config the server and connection
server = Server(ldap_server, port=int(636), use_ssl=bool(True))
conn = Connection(server=server, user=bind_dn, password=bind_pass)
#First make a touchbase in the LDAP Server with the credentials to authenticate
connection_status = conn.bind()
print("Status: ",connection_status)
# If the user and pass is correct it will continue the script
if connection_status == True:
#Filter the search to Groups
search_filter = '(objectClass=group)'
try:
#CHANGE TO YOUR GROUP SEARCH HERE
#This search will return a members list of the selected group
conn.search("CN=CI/OSR-NA Staff,OU=Recipients,OU=MAIL34,OU=DL,OU=MSX,DC=us,DC=bosch,DC=com",
search_filter, search_scope=SUBTREE, attributes=['member'])
members = []
#Set the list in a variable
for entry in conn.entries:
members = entry.member.values
print("\nGroup Members: \n\n", members, "\n")
status = "Permission Denied"
#Check if the user is part of the group
for member in members:
#If the user is part of the group it will return "Permission Allowed" and terminate the script.
if user.lower() in member.lower() or user.upper() in member.upper():
status = "Permission Allowed"
return status
#If the user is not part of the group it will return "Permission Denied" and terminate the script.
if status == "Permission Denied":
return status
except Exception as e:
return e
# If the user and pass is incorrect it will return "False" and terminate the script.
elif connection_status == False:
return "Connection error"
表示:如果字符串a
有值,则使用字符串b
。在您的例子中,因为第一个字符串是常量字符串,所以它总是有一个值,所以结果总是第二个字符串
a and b
意思是:如果字符串a
有值,则使用该值。否则,请使用字符串b
。在您的例子中,因为第一个字符串是常量字符串,所以它总是有一个值,以便始终使用第一个字符串
这种语法对于静态字符串毫无意义,因为结果总是相同的。您通常将此语法用于变量。你可以通过阅读来了解这些非常有用的案例
您需要将其设置为不带和或或的单个字符串。您还没有显示使用这些变量的代码,我也无法从下面的代码中找到它。但是如果您使用base\u dn
进行身份验证,那么您所做的将取决于LDAP服务器正在运行的内容。如果您使用的是Active Directory,我知道它只接受用户名-您不需要完整的DN。因此,如果您正在使用AD,您可以这样做:
a or b
我已经在下面添加了我的全部代码,让您看看我是如何使用它的。bind\u dn=用户无法在my中工作case@FarazGul服务器是否为Active Directory?服务器的目录未链接到整个Active Directory。但它使用的是Active Directory中的文件夹。如果它是AD,则可以使用bind\u dn=user
。如果您需要将应用程序仅限于这两个OU中的一个用户,则可以在连接后搜索用户名(“(sAMAccountName=“+user+”)
),并查看区分名称
,以验证帐户是否位于这两个OU中的一个OU中。这是唯一的方法,因为如果你想在bind\u DN
中提供一个完整的DN,那么你必须在连接之前知道用户在哪个OU中,而你无法知道。我需要的用户在上面这些OU中所述的两个不同的OU中,我需要那里的所有用户。如果不输入用户名,LDAP代码无法搜索整个OU?我已经在下面添加了我的全部代码,让您看看我是如何使用它的。bind\u dn=用户无法在my中工作case@FarazGul服务器是否为Active Directory?服务器的目录未链接到整个Active Directory。但它使用的是Active Directory中的文件夹。如果它是AD,则可以使用bind\u dn=user
。如果您需要将应用程序仅限于这两个OU中的一个用户,则可以在连接后搜索用户名(“(sAMAccountName=“+user+”)
),并查看区分名称
,以验证帐户是否位于这两个OU中的一个OU中。这是唯一的方法,因为如果你想在bind\u DN
中提供一个完整的DN,那么你必须在连接之前知道用户在哪个OU中,而你无法知道。我需要的用户在上面这些OU中所述的两个不同的OU中,我需要那里的所有用户。没有办法让LDAP代码在不输入用户名的情况下搜索整个OU?