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
#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


Messages In This Thread
Ldap Search for finding user Groups - by ilknurg - Mar-11-2022, 11:39 AM
RE: Ldap Search for finding user Groups - by DeaD_EyE - Mar-11-2022, 12:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Random student selection from groups. esahan 7 1,686 Jul-08-2024, 12:28 AM
Last Post: AdamHensley
  How to group related products in relationship groups? RegionHUser 2 923 Jun-02-2024, 03:51 PM
Last Post: Pedroski55
  Connecting to LDAP through Python ranjansanyal2007 0 886 Oct-09-2023, 05:21 PM
Last Post: ranjansanyal2007
  Make Groups with the List Elements quest 2 2,669 Jul-11-2021, 09:58 AM
Last Post: perfringo
  LDAP search for getmail sharbich 0 2,449 May-04-2021, 09:58 AM
Last Post: sharbich
  Understanding Regex Groups matt_the_hall 5 4,069 Jan-11-2021, 02:55 PM
Last Post: matt_the_hall
  How to solve equations, with groups of variables and or constraints? ThemePark 0 2,201 Oct-05-2020, 07:22 PM
Last Post: ThemePark
  Create homogeneous groups with Kmeans ? preliator 0 2,030 Sep-01-2020, 02:29 PM
Last Post: preliator
  Regex: finding if three groups have a value in them Daring_T 7 4,738 May-15-2020, 12:27 AM
Last Post: Daring_T
  How to take group of numbers summed in groups of 3... jaguare22 1 2,069 May-05-2020, 05:23 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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