pythonからldap

*pythonからldap

pythonからldap問い合わせ。

pipでldap3をインストール

# pip3 install ldap3

使い方

通常通りimportして検索。検索結果は標準出力にでるわけじゃないみたい。

$ python
>>> from ldap3 import Server,Connection,ALL,NTLM,SUBTREE

#サーバを指定  
>>> server=Server('ip_address',get_info=ALL)

#接続
>>> conn=Connection(server,user='Domain\UserName',password='passwrod',check_names=True,read_only=True,auto_bind=True)

#サーチ
>>> conn.search('dc=example,dc=co,dc=jp','(objectclass=person)',attribute=['cn'])

#出力
>>> conn.entries

conn.searchの一番目の引数は場所の指定、二番目が検索結果、三番目がどの属性を調べるかの指定。