Introduce groups to be able to manage more users
This commit is contained in:
@@ -1,15 +1,26 @@
|
||||
keys:
|
||||
john-doe: ssh-rsa XXXXXXXXX
|
||||
jane-doe: ssh-rsa YYYYYYYYY
|
||||
|
||||
groups:
|
||||
admin:
|
||||
- john-doe
|
||||
marketing:
|
||||
- jane-doe
|
||||
|
||||
hosts:
|
||||
- host: google.com
|
||||
users:
|
||||
- root
|
||||
- admin
|
||||
root:
|
||||
groups:
|
||||
- admin
|
||||
other-ssh-user:
|
||||
groups:
|
||||
- marketing
|
||||
keys:
|
||||
- john-doe
|
||||
- host: amazon.com
|
||||
users:
|
||||
- root
|
||||
keys:
|
||||
- name: johndoe@gmail.com
|
||||
description: John Doe
|
||||
key: ssh-rsa XXXXXXXXX
|
||||
- name: janedoe@gmail.com
|
||||
description: Jane Doe
|
||||
key: ssh-rsa YYYYYYYYY
|
||||
root:
|
||||
groups:
|
||||
- admin
|
||||
|
||||
30
monitor.py
30
monitor.py
@@ -5,14 +5,13 @@ import threading
|
||||
import yaml
|
||||
|
||||
class task_thread(threading.Thread):
|
||||
def __init__(self, host, user, keys, host_length):
|
||||
def __init__(self, host, user, host_length):
|
||||
threading.Thread.__init__(self)
|
||||
self.host = host
|
||||
self.user = user
|
||||
self.keys = keys
|
||||
self.host_length = host_length
|
||||
def run(self):
|
||||
update_keys(self.host, self.user, self.keys, self.host_length)
|
||||
load_metrics(self.host, self.user, self.host_length)
|
||||
|
||||
def read_config():
|
||||
with open('config.yaml', 'r') as stream:
|
||||
@@ -45,7 +44,7 @@ def parse_top_string(data):
|
||||
|
||||
return load, cpu_percent, ram_total, ram_free
|
||||
|
||||
def update_keys(host, user, keys, host_length):
|
||||
def load_metrics(host, user, host_length):
|
||||
try:
|
||||
client = paramiko.SSHClient()
|
||||
client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy())
|
||||
@@ -70,27 +69,20 @@ def main():
|
||||
|
||||
host_length = 0
|
||||
for host in config['hosts']:
|
||||
if host.get('users') == None:
|
||||
host['users'] = ['root']
|
||||
for user in host['users']:
|
||||
for user in host['users'].keys():
|
||||
if len(user) + len(host['host']) > host_length:
|
||||
host_length = len(user) + len(host['host'])
|
||||
|
||||
keys = []
|
||||
for key in config['keys']:
|
||||
keys.append(key['key'])
|
||||
|
||||
print('Host'.center(host_length + 3) + ' ' + 'Load'.center(25) + ' ' + 'Ram Usage'.center(26))
|
||||
|
||||
for host in config['hosts']:
|
||||
if host.get('users') == None:
|
||||
host['users'] = ['root']
|
||||
for user in host['users']:
|
||||
try:
|
||||
thread = task_thread(host['host'], user, keys, host_length)
|
||||
thread.start()
|
||||
except:
|
||||
print('❌ ' + user + '@' + host['host'])
|
||||
if 'root' not in host['users'].keys():
|
||||
continue
|
||||
try:
|
||||
thread = task_thread(host['host'], 'root', host_length)
|
||||
thread.start()
|
||||
except:
|
||||
print('❌ ' + user + '@' + host['host'])
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
39
sync.py
39
sync.py
@@ -22,29 +22,44 @@ def update_keys(host, user, keys):
|
||||
client = paramiko.SSHClient()
|
||||
client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy())
|
||||
client.connect(host, username = user, timeout = 1)
|
||||
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.exec_command('echo "###\n# Warning this file has been generated and will be overwritten!\n###\n' + '\n'.join(keys) + '" > ~/.ssh/authorized_keys2')
|
||||
client.close()
|
||||
print('✅ ' + user + '@' + host)
|
||||
except Exception:
|
||||
print('❌ ' + user + '@' + host)
|
||||
|
||||
def find_by_name(name, elements):
|
||||
found = [element for element in elements if element['name'] == name]
|
||||
if not found:
|
||||
return False
|
||||
return found[0]
|
||||
|
||||
def main():
|
||||
config = read_config()
|
||||
|
||||
keys = []
|
||||
|
||||
for key in config['keys']:
|
||||
keys.append(key['key'])
|
||||
|
||||
for host in config['hosts']:
|
||||
if host.get('users') == None:
|
||||
host['users'] = ['root']
|
||||
for user in host['users']:
|
||||
for user_name, user_data in host['users'].items():
|
||||
host_keys = []
|
||||
if 'groups' in user_data.keys():
|
||||
for group in user_data['groups']:
|
||||
if group not in config['groups'].keys():
|
||||
print('WARNING: Key-group "' + group + '" not found!')
|
||||
continue
|
||||
for key_name in config['groups'][group]:
|
||||
host_keys.append(config['keys'][key_name])
|
||||
if 'keys' in user_data.keys():
|
||||
for key_name in user_data['keys']:
|
||||
if key_name not in config['keys'].keys():
|
||||
print('WARNING: Key "' + key_name + '" not found!')
|
||||
continue
|
||||
host_keys.append(config['keys'][key_name])
|
||||
host_keys = list(set(host_keys)) # Filter duplicates
|
||||
if not host_keys:
|
||||
continue
|
||||
try:
|
||||
thread = task_thread(host['host'], user, keys)
|
||||
thread = task_thread(host['host'], user_name, host_keys)
|
||||
thread.start()
|
||||
except:
|
||||
print('❌ ' + user + '@' + host['host'])
|
||||
print('❌ ' + user_name + '@' + host['host'])
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user