Skip to content

Instantly share code, notes, and snippets.

@sochotnicky
Created September 24, 2018 13:16
Show Gist options
  • Save sochotnicky/988fca6dd9785f3253a43a4c81a0820b to your computer and use it in GitHub Desktop.
Save sochotnicky/988fca6dd9785f3253a43a4c81a0820b to your computer and use it in GitHub Desktop.
bin/ssh_keys_for_group.py
import sys
import ldap
l = ldap.initialize('ldap://ldap.corp.redhat.com')
groups = l.search_s('dc=redhat,dc=com', ldap.SCOPE_SUBTREE,
'cn=%s' % sys.argv[1])
no_keys = []
all_users = []
for group in groups:
query = '(|(uid=%s))' % ')(uid='.join(group[1]['memberUid'])
users = l.search_s('dc=redhat,dc=com', ldap.SCOPE_SUBTREE,
query)
all_users.extend([uid['uid'][0] for _,uid in users])
for cn, user in users:
if 'ipaSshPubKey' in user:
for key in user['ipaSshPubKey']:
print key
else:
no_keys.append(user['uid'][0])
print "All users in group: %s" % ", ".join(all_users)
print "Users without ssh keys in ldap: %s" % ", ".join(no_keys)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment