Python Forum
Ldap Search for finding user Groups
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ldap Search for finding user Groups
#1
I want to do ldap search for finding users groups.

I have this. I doesnt totally give me the result that i want but it works. If you have a query that more useful please inform me.

ldapsearch -x -H "ldap://192.168.1.240:3268" -D "Administrator@myserver" -w "mypassword" -b "dc=my,dc=server" '(sAMAccountName=user1)'
I want to run this command in my python code. I have the code

import os

cmd = ' ldapsearch -x -H "ldap://192.168.1.240:3268" -D "Administrator@myserverl" -w "mypassword" -b "dc=my, dc=server" '(sAMAccountName=user1)' '


print(os.system(cmd))
It gives me the error


^
SyntaxError: invalid syntax
Reply
#2
You can use shlex.split to split the str, like the shell does it.
The SyntaxError is raised because the quotes are wrong. If you have mixed single quotes and double quotes in one string,
you can use triple single quotes or triple double quotes. Example with triple double quotes:
name = """This "is" " ' A ' Test''' """
But this does not address the problem itself. The use of os.system should be avoided.


import shlex


command = shlex.split("""   censored   '""")

print(command)
Output:
['ldapsearch', ...]
os.system should not be used because it's limited (stdout does not return) and it has security issues.

Instead, use subprocess.Popen or subprocess.check_output. subprocess.check_output
raises a CalledProcessError if the called process returns with non-zero. If the program was not found, a FileNotFoundError is raised. If the program exists, but you're not allowed to run the program (missing execute permission), a PermissionError is raised.



from subprocess import CalledProcessError, check_output


def search():
    cmd = [
        'ldapsearch',
        ...
    ]
    try:
        return check_output(cmd, encoding="utf8")
    except CalledProcessError:
        pass
    except FileNotFoundError:
        raise RuntimeError("The program `ldapsearch` is not installed.")
Then you can use the function also to submit different addresses to your ldapsearch.

from subprocess import CalledProcessError, check_output


def search(ip, port=3268):
    cmd = [
        'ldapsearch',
        '-H',
        f'ldap://{ip}:{port}',
        ...,
    ]
    try:
        return check_output(cmd, encoding="utf8")
    except CalledProcessError:
        pass
    except FileNotFoundError:
        raise RuntimeError("The program `ldapsearch` is not installed.")
ilknurg likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Connecting to LDAP through Python ranjansanyal2007 0 420 Oct-09-2023, 05:21 PM
Last Post: ranjansanyal2007
  Make Groups with the List Elements quest 2 1,983 Jul-11-2021, 09:58 AM
Last Post: perfringo
  LDAP search for getmail sharbich 0 1,808 May-04-2021, 09:58 AM
Last Post: sharbich
  Understanding Regex Groups matt_the_hall 5 2,837 Jan-11-2021, 02:55 PM
Last Post: matt_the_hall
  How to solve equations, with groups of variables and or constraints? ThemePark 0 1,690 Oct-05-2020, 07:22 PM
Last Post: ThemePark
  Create homogeneous groups with Kmeans ? preliator 0 1,541 Sep-01-2020, 02:29 PM
Last Post: preliator
  Regex: finding if three groups have a value in them Daring_T 7 3,364 May-15-2020, 12:27 AM
Last Post: Daring_T
  How to take group of numbers summed in groups of 3... jaguare22 1 1,510 May-05-2020, 05:23 AM
Last Post: Yoriz
  Python3/Flask/LDAP-Active Directory tundrwd 1 36,691 Dec-13-2019, 05:48 PM
Last Post: Clunk_Head
  Listing groups tharpa 2 2,583 Nov-26-2019, 07:25 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020