Add password_cmd parameter

This commit is contained in:
Benjamin Collet 2022-01-10 10:29:28 +01:00
parent 5a95b4ecdc
commit 9ddb8a4eb9
No known key found for this signature in database
GPG key ID: DF2423FD91470C0E
2 changed files with 18 additions and 12 deletions

View file

@ -5,6 +5,7 @@ import yaml
import argparse import argparse
import os import os
import shutil import shutil
import subprocess
config = os.path.join(os.path.dirname(os.path.realpath(__file__)),"config.yml") 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): def call_api(params, path):
url = '/'.join((params['base_url'],params['api_path'],path)) url = '/'.join((params['base_url'],params['api_path'],path))
try: try:
response = requests.get(url, auth=(params['username'], if 'password_cmd' in params:
params['password'])) 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: except:
return return
@ -49,15 +54,6 @@ def sizeof_fmt(num, suffix='B', binary=True):
num /= base num /= base
return "%.1f %s%s" % (num, 'Y', suffix) 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): def search_ports(args):
for instance, params in cfg['instances'].items(): for instance, params in cfg['instances'].items():
if args.instance is not None and instance not in args.instance.split(','): 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") 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 # Argument parsing
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-i','--instance', type=str, help="Comma-separated list of Observium instances to use",nargs='?', default=None) parser.add_argument('-i','--instance', type=str, help="Comma-separated list of Observium instances to use",nargs='?', default=None)

View file

@ -4,7 +4,7 @@ instances:
base_url: 'https://observium1' base_url: 'https://observium1'
api_path: 'api/v0' api_path: 'api/v0'
username: '' username: ''
password: '' password_cmd: ''
observium2: observium2:
base_url: 'http://observium2' base_url: 'http://observium2'
api_path: 'api/v0' api_path: 'api/v0'