From bbf66681a90a588a288cb0d2a812e7a8ba0706ba Mon Sep 17 00:00:00 2001 From: Olivier Blin Date: Thu, 23 Feb 2017 01:29:21 +0100 Subject: ldap-sshkey2file.py: use argparse for options parsing and usage --- modules/openssh/templates/ldap-sshkey2file.py | 31 +++++++++++++-------------- 1 file changed, 15 insertions(+), 16 deletions(-) (limited to 'modules') diff --git a/modules/openssh/templates/ldap-sshkey2file.py b/modules/openssh/templates/ldap-sshkey2file.py index f01a3fbf..7c41832d 100755 --- a/modules/openssh/templates/ldap-sshkey2file.py +++ b/modules/openssh/templates/ldap-sshkey2file.py @@ -5,6 +5,8 @@ import os import random import shutil import tempfile +import argparse +import textwrap try: import ldap @@ -29,18 +31,19 @@ nslcd_conf_file="<%= nslcd_conf_file %>" filter="(&(objectClass=inetOrgPerson)(objectClass=ldapPublicKey)(objectClass=posixAccount)(sshPublicKey=*))" keypathprefix='/home' -def usage(): - print "%s" % sys.argv[0] - print - print "Will fetch all enabled user accounts under %s" % peopledn - print "with ssh keys in them and write each one to" - print "%s//.ssh/authorized_keys" % keypathprefix - print - print "It will return failure when no keys are updated and success" - print "when one or more keys have changed." - print - print "This script is intended to be run from cron as root" - print +parser = argparse.ArgumentParser( + formatter_class=argparse.RawDescriptionHelpFormatter, + description=textwrap.dedent('''\ + Will fetch all enabled user accounts under %s + with ssh keys in them and write each one to + %s//.ssh/authorized_keys + + It will return failure when no keys are updated and success + when one or more keys have changed. + + This script is intended to be run from cron as root; + ''' % (peopledn, keypathprefix))) +args = parser.parse_args() def get_bindpw(): try: @@ -134,10 +137,6 @@ def write_keys(keys, user, uid, gid): return True -if len(sys.argv) != 1: - usage() - sys.exit(1) - bindpw = get_bindpw() changed = False -- cgit v1.2.1