#!/usr/bin/python from os.path import dirname, realpath import getopt, sys sys.path.append(dirname(realpath(__file__)) + '/lib') import getpass from LdapTree import LdapTree def usage(): print "Usage: " + sys.argv[0] + " OPTION...\n" print "Create a tree representation of all DNs starting with a given base DN." print "Only simple binds to the directory with DN and password are supported." print "If no password OPTION is given the password will be asked interactive." print "If no outfile the given the result will be written to stdout.\n" print "Required OPTIONS are:\n" print " {:30s} : {:s}".format('-H, --hosturi=URI', 'The URI to the ldap server to query in the form:') print " {:30s} {:s}".format('', 'ldap[s]://host.uri[:port]') print " {:30s} : {:s}".format('-D, --binddn=DN', 'The DN to use for the LDAP bind.') print " {:30s} : {:s}\n".format('-b, --basedn=DN', 'The DN to start the tree with.') print "Optional OPTIONS are:\n" print " {:30s} : {:s}".format('-h, --help', 'Show this help page') print " {:30s} : {:s}".format('-p, --password=PASSWORD', 'The password to use for the LDAP bind.') print " {:30s} : {:s}".format('-o, --outfile=FILENAME', 'File to write the result to.') print " {:30s} : {:s}".format('-k, --kerberos', 'Use gssapi auth.') def main(): try: opts, args = getopt.getopt( sys.argv[1:], 'hkgH:D:b:p:o:', ['help', 'kerberos', 'hosturi=', 'binddn=', 'basedn=', 'password=', 'outfile=']) except getopt.GetoptError as err: print str(err) usage() sys.exit(2) hosturi = binddn = basedn = password = outfile = None creategraph = False use_gssapi = False for o, a in opts: if o in ["-h", "--help"]: usage() sys.exit(0) elif o in ["-H", "--hosturi"]: hosturi = a elif o in ["-D", "--binddn"]: binddn = a elif o in ["-b", "--basedn"]: basedn = a elif o in ["-p", "--password"]: password = a elif o in ["-o", "--outfile"]: outfile = a elif o == "-g": creategraph = True elif o in ["-k", "--kerberos"]: use_gssapi = True; else: print "unknown parameter: " + a usage() sys.exit(2) if not hosturi or (not binddn and not use_gssapi) or not basedn: usage() sys.exit(2) if not password and not use_gssapi: password = getpass.getpass() info = LdapTree(hosturi, binddn, basedn, password, use_gssapi) if not creategraph: if outfile: info.text(outfile) else: print info.text() else: if outfile: info.graph(outfile) else: print info.graph() if __name__ == "__main__": main()