Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python
# -*- coding: utf-8-*-
# python-ldapを使ってActiveDirectoryで認証してみる
# http://python-ldap.sourceforge.net/
def ADAuth(username,password,host,port=389):
"""ActiveDirectoryのドメイコントローラにユーザ名とパスワードでbindしてみる。
bindできれば認証OK。認証NGなら例外が起きる。
"""
import ldap
url = "ldap://%s:%d" % (host,port)
l = ldap.initialize(url)
l.simple_bind_s(username,password)
l.unbind_s()
if __name__=='__main__':
if len(sys.argv)<3:
print "Usage: %s username password" % (sys.argv[0])
else:
try:
userid = sys.argv[1]
password = sys.argv[2]
ADAuth(userid,password,"adserver.example.com")
print "OK"
except:
print "NG"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment