diff --git a/client.py b/client.py index 2fdf629..d118a6a 100755 --- a/client.py +++ b/client.py @@ -5,6 +5,7 @@ import yaml import argparse import os import shutil +import subprocess config = os.path.join(os.path.dirname(os.path.realpath(__file__)),"config.yml") @@ -16,8 +17,12 @@ term_cols = getattr(shutil.get_terminal_size((80, 20)), 'columns') def call_api(params, path): url = '/'.join((params['base_url'],params['api_path'],path)) try: - response = requests.get(url, auth=(params['username'], - params['password'])) + if 'password_cmd' in params: + result = subprocess.run(params['password_cmd'].split(), stdout=subprocess.PIPE) + password = result.stdout.decode('utf-8') + else: + password = params['password'] + response = requests.get(url, auth=(params['username'],password)) except: return @@ -49,15 +54,6 @@ def sizeof_fmt(num, suffix='B', binary=True): num /= base return "%.1f %s%s" % (num, 'Y', suffix) -# Test instances -for instance in list(cfg['instances']): - test = call_api(cfg['instances'][instance], 'devices') - if test is None: - if cfg['debug']: print("Instance %s is not working, disabling" % instance) - cfg['instances'].pop(instance) - else: - if cfg['debug']: print("Instance %s is working" % instance) - def search_ports(args): for instance, params in cfg['instances'].items(): if args.instance is not None and instance not in args.instance.split(','): @@ -198,6 +194,16 @@ def search_devices(args): print("(0m" + "q" * 27 + "v" + "q" * 28 + "v" + "q" * 28 + "v" + "q" * (term_cols - 88) + "j(B") +# Test instances +for instance in list(cfg['instances']): + test = call_api(cfg['instances'][instance], 'devices') + if test is None: + if cfg['debug']: print("Instance %s is not working, disabling" % instance) + cfg['instances'].pop(instance) + else: + if cfg['debug']: print("Instance %s is working" % instance) + + # Argument parsing parser = argparse.ArgumentParser() parser.add_argument('-i','--instance', type=str, help="Comma-separated list of Observium instances to use",nargs='?', default=None) diff --git a/config.yml.dist b/config.yml.dist index ad9a688..185fb80 100644 --- a/config.yml.dist +++ b/config.yml.dist @@ -4,7 +4,7 @@ instances: base_url: 'https://observium1' api_path: 'api/v0' username: '' - password: '' + password_cmd: '' observium2: base_url: 'http://observium2' api_path: 'api/v0'