initial
This commit is contained in:
commit
a3fdb08a52
11
ReadMe.md
Normal file
11
ReadMe.md
Normal file
@ -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.
|
||||
11
genkeys.py
Normal file
11
genkeys.py
Normal file
@ -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)
|
||||
7
utils.py
Normal file
7
utils.py
Normal file
@ -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
|
||||
32
writekeys.py
Normal file
32
writekeys.py
Normal file
@ -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))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user