From a3fdb08a52a7ce09cb8a23e18fb68dafd79e7960 Mon Sep 17 00:00:00 2001 From: zmy Date: Wed, 12 May 2021 21:19:00 +0800 Subject: [PATCH] initial --- ReadMe.md | 11 +++++++++++ genkeys.py | 11 +++++++++++ users.txt | 2 ++ utils.py | 7 +++++++ writekeys.py | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 63 insertions(+) create mode 100644 ReadMe.md create mode 100644 genkeys.py create mode 100644 users.txt create mode 100644 utils.py create mode 100644 writekeys.py diff --git a/ReadMe.md b/ReadMe.md new file mode 100644 index 0000000..ae24f78 --- /dev/null +++ b/ReadMe.md @@ -0,0 +1,11 @@ +# PublicKey Configurator. +Generate keys and configure keys for given users. + +# How to use. +1. write the username to users.txt, one name per line. +2. run genkeys.py, it will generate ssh key pairs for all the users in the users.txt +3. run writekeys.py, it will write the generated the keys to $HOME/.ssh/authorized_keys. +4. send the generated private key to the user. + +# Notice. +if the user has already configured a proper sshkey, it will skip it. \ No newline at end of file diff --git a/genkeys.py b/genkeys.py new file mode 100644 index 0000000..c5d265f --- /dev/null +++ b/genkeys.py @@ -0,0 +1,11 @@ +import os +from utils import load_users + +users = load_users('./users.txt') + +command = 'ssh-keygen -b 1024 -N "" -C "" -f {0}' + +for user in users: + if not os.path.isfile(user): + this_command = command.format(user) + os.system(this_command) diff --git a/users.txt b/users.txt new file mode 100644 index 0000000..84cd7a6 --- /dev/null +++ b/users.txt @@ -0,0 +1,2 @@ +usernamehere +oneuserperline diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..58e7311 --- /dev/null +++ b/utils.py @@ -0,0 +1,7 @@ +def load_users(path): + with open(path, 'r', encoding='utf-8') as f: + users = f.read() + users = users.split('\n') + users = [u.strip() for u in users] + users = [u for u in users if u != ''] + return users \ No newline at end of file diff --git a/writekeys.py b/writekeys.py new file mode 100644 index 0000000..c64dc73 --- /dev/null +++ b/writekeys.py @@ -0,0 +1,32 @@ +import os +from utils import load_users + +users = load_users('./users.txt') + +home = '/home/{0}' + +def excecute(command, prefix=' '): + print(prefix + 'executing', command) + os.system(command) + + +for user in users: + userhome = home.format(user) + if os.path.isdir(userhome): + print('Writing keys for user {0}'.format(user)) + # print('checking ', os.path.join(userhome, '.ssh')) + ssh_dir = os.path.join(userhome, '.ssh') + if not os.path.isdir(ssh_dir): + print('.ssh directory do not exist, creating...') + excecute('mkdir {0}'.format(ssh_dir)) + excecute('chown -R {0}:{1} {2}'.format(user, user, ssh_dir)) + + authorized_keys = os.path.join(userhome, '.ssh', 'authorized_keys') + if os.path.isfile(authorized_keys): + print(' keyfile for user {0} already exist, no operation will take.'.format(user)) + else: + print(' Writing keyfiles...') + excecute('cp {0}.pub {1}'.format(user, authorized_keys)) + print(' changing user privelege...') + excecute('chown {0}:{1} {2}'.format(user, user, authorized_keys)) +