From: Frank Brehm Date: Mon, 5 Dec 2011 22:52:17 +0000 (+0100) Subject: Initial X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=1beeab50eaed4920b1bd9526b3fd18eb3cb7b623;p=my-stuff%2Fpython.git Initial --- 1beeab50eaed4920b1bd9526b3fd18eb3cb7b623 diff --git a/get-ssh-keys.py b/get-ssh-keys.py new file mode 100755 index 0000000..5edd294 --- /dev/null +++ b/get-ssh-keys.py @@ -0,0 +1,124 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +''' +@author: Frank Brehm +@contact: frank.brehm@profitbricks.com +@license: GPL3 +@copyright: (c) 2010-2011 by Frank Brehm, Berlin +@version: 0.1.0 +@summary: Try to get SSH-Keys from LDAP +''' + +import ldap +from ldap import LDAPError +import string +from traceback import print_exc +import pprint +from time import sleep +import signal +import errno + +server = "staging" +bind_dn = 'cn=admin,dc=profitbricks,dc=localdomain' +bind_pw = 'NalabNer4' +base_dn = 'dc=profitbricks,dc=localdomain' +search_item = 'de-blnstage-c1-pserver5' +filter_template = "(&(objectClass=ldapPublicKey)(cn=%s))" + + +#======================================================================== + +def search_cn(l, keyword): + + scope = ldap.SCOPE_SUBTREE + filter_arg = "cn=" + "*" + keyword + "*" + filter_arg = "cn=" + keyword + retrieve_attributes = None + + filter_arg = filter_template % (search_item) + + count = 0 + result_set = [] + timeout = 1 + print "Search filter: '%s'..." %(filter_arg) + + pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 ) + + def read_alarm_caller(signum, sigframe): + raise IOError( + errno.ETIMEDOUT, + 'Could not read from %s after %s second(s)' + %( server, timeout ) + ) + + timeout = int(timeout) + + try: + result_id = l.search(base_dn, scope, filter_arg, retrieve_attributes) + print result_id + while 1: + signal.signal(signal.SIGALRM, read_alarm_caller) + signal.alarm(timeout) + result_type, result_data = l.result(result_id, 0) + signal.alarm(0) + print "Result: " + pp.pformat( (result_type, result_data) ) + sleep(0.2) + if (result_data == []): + break + else: + if result_type == ldap.RES_SEARCH_ENTRY: + result_set.append(result_data) + + if len(result_set) == 0: + print "No Results." + return + + for i in range(len(result_set)): + for entry in result_set[i]: + try: + name = entry[1]['cn'][0] + email = entry[1]['mail'][0] + phone = entry[1]['telephonenumber'][0] + desc = entry[1]['description'][0] + count = count + 1 + + print "%d.\nName: %s\nDescription: %s\nE-mail: %s\nPhone: %s\n" %\ + (count, name, desc, email, phone) + except: + pass + + except: + print_exc() + + +#------------------------------------------------------ +def main(): + + url = 'ldap://' + server + '/' + + try: + #l = ldap.open(server) + print "Trying to initialize to '%s'..." %(url) + l = ldap.initialize(url) + l.simple_bind_s(bind_dn, bind_pw) + print( "Successfully bound to server '%s' as '%s'.\n" + % (server, bind_dn) ) + + print "Searching..\n" + search_cn(l, search_item) + + except ldap.LDAPError, error_message: + print "Couldn't Connect. %s " % error_message + except: + print_exc() + +#------------------------------------------------------ + +if __name__ == "__main__": + main() + + +#======================================================================== + +# vim: fileencoding=utf-8 filetype=python ts=4 expandtab diff --git a/ldap-test.py b/ldap-test.py new file mode 100755 index 0000000..44847ae --- /dev/null +++ b/ldap-test.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +''' +@author: Frank Brehm +@contact: frank@brehm-online.com +@license: GPL3 +@copyright: (c) 2010-2011 by Frank Brehm, Berlin +@version: 0.1.0 +@summary: Try to get some informations from LDAP +''' + +import ldap +#from ldap import LDAPError +import string +from traceback import print_exc + +server = "ldap.brehm-online.com" +bind_dn = 'cn=frank,dc=brehm-online,dc=com' +bind_pw = 'up2UdLCE' +base_dn = 'ou=Users,dc=brehm-online,dc=com' +search_item = 'frank' + + +#======================================================================== + +def search_cn(l, keyword): + + scope = ldap.SCOPE_SUBTREE + filter_arg = "cn=" + "*" + keyword + "*" + filter_arg = "cn=" + keyword + retrieve_attributes = None + + count = 0 + result_set = [] + timeout = 5 + print "Search filter: '%s'..." %(filter_arg) + + try: + result_id = l.search(base_dn, scope, filter_arg, retrieve_attributes) + while 1: + result_type, result_data = l.result(result_id, timeout) + if (result_data == []): + break + else: + if result_type == ldap.RES_SEARCH_ENTRY: + result_set.append(result_data) + + if len(result_set) == 0: + print "No Results." + return + + for i in range(len(result_set)): + for entry in result_set[i]: + try: + name = entry[1]['cn'][0] + email = entry[1]['mail'][0] + phone = entry[1]['telephonenumber'][0] + desc = entry[1]['description'][0] + count = count + 1 + + print "%d.\nName: %s\nDescription: %s\nE-mail: %s\nPhone: %s\n" %\ + (count, name, desc, email, phone) + except: + pass + + except: + print_exc() + + +#------------------------------------------------------ +def main(): + + url = 'ldap://' + server + '/' + + try: + #l = ldap.open(server) + print "Trying to initialize to '%s'..." %(url) + l = ldap.initialize(url) + l.simple_bind_s(bind_dn, bind_pw) + print( "Successfully bound to server '%s' as '%s'.\n" + % (server, bind_dn) ) + + print "Searching..\n" + search_cn(l, search_item) + + + except: + print_exc() + #except ldap.LDAPError, error_message: + # print "Couldn't Connect. %s " % error_message + +#------------------------------------------------------ + +if __name__ == "__main__": + main() + +#======================================================================== + +# vim: fileencoding=utf-8 filetype=python ts=4 expandtab