From d78319da0811d6e3583b2be48d8ac7063d713843 Mon Sep 17 00:00:00 2001 From: Tobias Schneider Date: Thu, 4 Mar 2021 21:37:06 +0100 Subject: [PATCH] Initial commit --- .gitignore | 32 +++++++++++++++++++++++++++++++ README.md | 7 +++++++ config-example.yaml | 10 ++++++++++ requirements.txt | 2 ++ sync.py | 46 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 config-example.yaml create mode 100644 requirements.txt create mode 100755 sync.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..50be0e0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,32 @@ +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# Python +__pycache__/ + +# Project specific +config.yaml \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..0d69d31 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Sync SSH Keys + +## Setup +Rename `config-example.yaml` to `config.yaml` and set your own data. + +## Usage +Run `./sync.py` or `python3 sync.py` \ No newline at end of file diff --git a/config-example.yaml b/config-example.yaml new file mode 100644 index 0000000..eaedd0d --- /dev/null +++ b/config-example.yaml @@ -0,0 +1,10 @@ +hosts: + - google.com + - amazon.com +keys: + - name: johndoe@gmail.com + description: John Doe + key: ssh-rsa XXXXXXXXX + - name: janedoe@gmail.com + description: Jane Doe + key: ssh-rsa YYYYYYYYY \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ec656b3 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +PyYAML==5.4.1 +paramiko==2.7.2 \ No newline at end of file diff --git a/sync.py b/sync.py new file mode 100755 index 0000000..1660457 --- /dev/null +++ b/sync.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +import paramiko +import threading +import yaml + +class taskThread(threading.Thread): + def __init__(self, host, keys): + threading.Thread.__init__(self) + self.host = host + self.keys = keys + def run(self): + updateKeys(self.host, self.keys) + +def readConfig(): + with open('config.yaml', 'r') as stream: + return yaml.safe_load(stream) + +def updateKeys(host, keys): + try: + client = paramiko.SSHClient() + client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy()) + client.connect(host, username = 'root') + 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) + except Exception: + print('❌ ' + host) + +def main(): + config = readConfig() + + keys = [] + + for key in config['keys']: + keys.append(key['key']) + + for host in config['hosts']: + try: + thread = taskThread(host, keys) + thread.start() + except: + print('❌ ' + host) + +if __name__ == '__main__': + main() \ No newline at end of file