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))