Last active
May 4, 2016 05:33
-
-
Save rako233/73ecab3302c3400fc49a20f7516db45f to your computer and use it in GitHub Desktop.
LDAP Test Of An ActiveDirectory Server With Python
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
import ldap | |
import ldif | |
import sys | |
def auth(address, username, password): | |
conn = ldap.initialize('ldap://' + address) | |
conn.protocol_version = 3 | |
conn.set_option(ldap.OPT_REFERRALS, 0) | |
try: | |
result = conn.simple_bind_s(username, password) | |
except ldap.INVALID_CREDENTIALS: | |
return "Invalid credentials",0 | |
except ldap.SERVER_DOWN: | |
return "Server down",0 | |
except ldap.LDAPError, e: | |
if type(e.message) == dict and e.message.has_key('desc'): | |
return "Other LDAP error: " + e.message['desc'],0 | |
else: | |
return "Other LDAP error: " + e,0 | |
conn.set_option(ldap.OPT_SIZELIMIT,10000) | |
return 'Success', conn | |
def myauth(): | |
msg, conn = auth('10.0.0.1','<user>','<password>') | |
print(msg) | |
return conn | |
conn = myauth() | |
ldif_writer = ldif.LDIFWriter(sys.stdout) | |
basedn = 'OU=allusers,DC=company,DC=com' | |
results = conn.search_s(basedn,ldap.SCOPE_SUBTREE,"(&(objectClass=user)(!(objectClass=computer)))") | |
conn.unbind_s() | |
for dn,entry in results: | |
ldif_writer.unparse(dn,entry) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment