Added multiuser support

This commit is contained in:
2021-07-04 00:51:59 +02:00
parent 306611a85e
commit 74637fe485
3 changed files with 36 additions and 26 deletions

24
sync.py
View File

@@ -5,27 +5,28 @@ import threading
import yaml
class task_thread(threading.Thread):
def __init__(self, host, keys):
def __init__(self, host, user, keys):
threading.Thread.__init__(self)
self.host = host
self.user = user
self.keys = keys
def run(self):
update_keys(self.host, self.keys)
update_keys(self.host, self.user, self.keys)
def read_config():
with open('config.yaml', 'r') as stream:
return yaml.safe_load(stream)
def update_keys(host, keys):
def update_keys(host, user, keys):
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy())
client.connect(host, username = 'root')
client.connect(host, username = user)
client.exec_command('echo "###\n# Warning this file has been generated and will be overwritten!\n###\n\n' + '\n'.join(keys) + '" > ~/.ssh/authorized_keys2')
client.close()
print('' + host)
print('' + user + '@' + host)
except Exception:
print('' + host)
print('' + user + '@' + host)
def main():
config = read_config()
@@ -36,11 +37,12 @@ def main():
keys.append(key['key'])
for host in config['hosts']:
try:
thread = task_thread(host, keys)
thread.start()
except:
print('' + host)
for user in host['users']:
try:
thread = task_thread(host['host'], user, keys)
thread.start()
except:
print('' + user + '@' + host['host'])
if __name__ == '__main__':
main()