simplebrowse.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #! python
  2. #
  3. # simple LDAP server browsing example
  4. #
  5. import ldap
  6. import string
  7. from traceback import print_exc
  8. url = "ldap://ldap.openldap.org/"
  9. dn = "dc=openldap,dc=org"
  10. print "Connecting to", url
  11. l = ldap.initialize(url)
  12. l.bind_s("", "", ldap.AUTH_SIMPLE);
  13. lastdn = dn
  14. dnlist = None
  15. while 1:
  16. #-- read a command
  17. try:
  18. cmd = raw_input(dn + "> ")
  19. except EOFError:
  20. print
  21. break
  22. try:
  23. if cmd == "?":
  24. print "cd <dn> - change DN to <dn>"
  25. print "cd <n> - change DN to number <n> of last 'ls'"
  26. print "cd - - change to previous DN"
  27. print "cd .. - change to one-level higher DN"
  28. print "cd - change to root DN"
  29. print "ls - list children of crrent DN"
  30. print ". - show attributes of current DN"
  31. print "/<expr> - list descendents matching filter <expr>"
  32. print "? - show this help"
  33. elif cmd == "ls":
  34. print "Children of", `dn`, ":"
  35. dnlist = []
  36. #
  37. # List the children at one level down from the current dn
  38. # We use the filter 'objectclass=*' to match everything.
  39. # We're not interested in attributes at this stage, so
  40. # we specify [] as the list of attribute names to retreive.
  41. #
  42. for name,attrs in l.search_s(dn, ldap.SCOPE_ONELEVEL,
  43. "objectclass=*", []):
  44. #-- shorten resulting dns for output brevity
  45. if name.startswith(dn+", "):
  46. shortname = "+ "+name[len(dn)+2:]
  47. elif name.endswith(", "+dn):
  48. shortname = name[:-len(dn)-2]+" +"
  49. else:
  50. shortname = name
  51. print " %3d. %s" % (len(dnlist), shortname)
  52. dnlist.append(name)
  53. elif cmd == "cd":
  54. dn = ""
  55. dnlist = None
  56. elif cmd.startswith("cd "):
  57. arg = cmd[3:]
  58. if arg == '-':
  59. lastdn,dn = dn,lastdn
  60. elif arg == '..':
  61. dn = string.join(ldap.explode_dn(dn)[1:], ",")
  62. dn = string.strip(dn)
  63. else:
  64. try:
  65. i = int(arg)
  66. except:
  67. godn = arg
  68. else:
  69. if dnlist is None:
  70. print "do an ls first"
  71. else:
  72. godn = dnlist[i]
  73. lastdn = dn
  74. dn = godn
  75. elif cmd == ".":
  76. #
  77. # Retrieve all the attributes for the current dn.
  78. # We construct a search using SCOPE_BASE (ie just the
  79. # given DN) and again filter with "objectclass=*".
  80. # No attributes are listed, so the default is for
  81. # the client to receive all attributes on the DN.
  82. #
  83. print "Attributes of", `dn`, ":"
  84. for name,attrs in l.search_s(dn, ldap.SCOPE_BASE,
  85. "objectclass=*"):
  86. print " %-24s" % name
  87. for k,vals in attrs.items():
  88. for v in vals:
  89. if len(v) > 200:
  90. v = `v[:200]` + \
  91. ("... (%d bytes)" % len(v))
  92. else:
  93. v = `v`
  94. print " %-12s: %s" % (k, v)
  95. elif cmd.startswith("/"):
  96. #
  97. # Search descendent objects to match a given filter.
  98. # We use SCOPE_SUBTREE to indicate descendents, and
  99. # again specify an empty attribute list to indicate
  100. # that we're not interested in them.
  101. #
  102. expr = cmd[1:]
  103. print "Descendents matching filter", `expr`, ":"
  104. for name,attrs in l.search_s(dn, ldap.SCOPE_SUBTREE,
  105. expr, []):
  106. print " %24s", name
  107. else:
  108. print "unknown command - try '?' for help"
  109. except:
  110. print_exc()