Last active
July 25, 2022 13:18
-
-
Save rsperl/efc0c78bceb41ccb189871a5402ddeae to your computer and use it in GitHub Desktop.
connect to ldap #python #ldap #snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import ldap | |
import sys | |
def get_connection(uri, username, password): | |
# or set env var LDAPTLS_REQCERT=never | |
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_ALLOW) | |
con = ldap.initialize(uri) | |
# stupid magic code - required before binding or you get strange errors about | |
# not being bound even though you are | |
# ref: http://stackoverflow.com/questions/18793040/python-ldap-not-able-to-bind-successfully | |
con.protocol_version = ldap.VERSION3 | |
con.set_option(ldap.OPT_REFERRALS, 0) | |
# end magic code | |
con.simple_bind_s(username.lower(), password) | |
# bind_result = con.bind(username, self.opts["password"]) | |
# print "bind result: " + str(bind_result) | |
return con | |
if len(sys.argv) != 4: | |
print("Usage: {} uri username password".format(sys.argv[0])) | |
sys.exit(1) | |
uri, username, password = sys.argv[1], sys.argv[2], sys.argv[3] | |
try: | |
con = get_connection(uri, username, password) | |
print("Connected ok") | |
except Exception as e: | |
print("Failed to connect to {} as {}: {}".format(uri, username, e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment