File: urlsearch.py

package info (click to toggle)
python-ldap 3.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,248 kB
  • sloc: python: 9,465; ansic: 2,828; makefile: 132; sh: 68
file content (32 lines) | stat: -rw-r--r-- 843 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""
Do a search with the LDAP URL specified at command-line.

No output of LDAP data is produced except trace output.
"""
from __future__ import print_function
import sys,getpass,ldap,ldapurl

try:
  ldapUrl = ldapurl.LDAPUrl(ldapUrl=sys.argv[1])
except IndexError:
  print('Usage: %s [LDAP URL]' % (sys.argv[0]))
  sys.exit(1)

for a in [
  'urlscheme','hostport','dn','attrs','scope',
  'filterstr','extensions','who','cred'
]:
  print(a,repr(getattr(ldapUrl,a)))

l = ldap.initialize(ldapUrl.initializeUrl(),trace_level=1)
if ldapUrl.who!=None:
  if ldapUrl.cred!=None:
    cred=ldapUrl.cred
  else:
    print('Enter password for simple bind with',repr(ldapUrl.who))
    cred=getpass.getpass()
  l.simple_bind_s(ldapUrl.who,cred)

res = l.search_s(ldapUrl.dn,ldapUrl.scope,ldapUrl.filterstr,ldapUrl.attrs)

print(len(res),'search results')