diff options
author | Alexander Sulfrian <alexander@sulfrian.net> | 2013-05-16 21:15:35 +0200 |
---|---|---|
committer | Alexander Sulfrian <alexander@sulfrian.net> | 2013-05-16 21:15:35 +0200 |
commit | 787f5f43cb6e8fb1a012115f979049375f6910b1 (patch) | |
tree | bb76a899efd5993bc38cde02fce5d3129e11d608 | |
parent | 9a09a2f1a5cd428abc84f615196f518cac3cdc9f (diff) | |
download | tools-787f5f43cb6e8fb1a012115f979049375f6910b1.tar.gz tools-787f5f43cb6e8fb1a012115f979049375f6910b1.tar.bz2 tools-787f5f43cb6e8fb1a012115f979049375f6910b1.zip |
bin/hostinfo: add --keys to get valid keys for a host
The bash completion uses now a dynamic list of valid keys for
a host and therefor calls the hostinfo tool itself.
-rwxr-xr-x | bin/hostinfo | 25 | ||||
-rw-r--r-- | contrib/bash-completion | 4 |
2 files changed, 26 insertions, 3 deletions
diff --git a/bin/hostinfo b/bin/hostinfo index 1b92fdf..391e7db 100755 --- a/bin/hostinfo +++ b/bin/hostinfo @@ -24,6 +24,26 @@ def print_info(path, key=None, oneline=False): p = printer.Printer(data, oneline) p.info(key) +def _print_keys(data, prefix = ''): + for key in data.keys(): + print "%s%s" % (prefix, key) + + if key == 'addresses': + for k in set([a['interface'] for a in data[key]]): + print "%s%s.%s" % (prefix, key, k) + if key == 'ports': + for k in set([p['process'] for p in data[key] if 'process' in p]): + print "%s%s.%s" % (prefix, key, k) + if len([p for p in data[key] if 'process' not in p]) > 0: + print "%s%s.%s" % (prefix, key, 'UNKNOWN') + + if isinstance(data[key], dict): + _print_keys(data[key], "%s%s." % (prefix, key)) + +def print_keys(path): + data = _get_data(path) + _print_keys(data) + def find_host(host): path = "/usr/local/share/hostinfo/%s" % host if os.path.exists(path): @@ -51,11 +71,12 @@ def find_host(host): def main(): self_name = sys.argv.pop(0) - optlist, args = getopt.gnu_getopt(sys.argv, 'of', ['oneline', 'file']) + optlist, args = getopt.gnu_getopt(sys.argv, 'ofk', ['oneline', 'file', 'keys']) flags = [opt for (opt, value) in optlist if value == ''] file = '--file' in flags or '-f' in flags oneline = '--oneline' in flags or '-o' in flags + keys = '--keys' in flags or '-k' in flags if len(args) < 1: print('Usage: %s <host> [info]' % self_name) @@ -68,6 +89,8 @@ def main(): if file: print(path) + elif keys: + print_keys(path) else: if len(args) == 1: print_info(path, oneline=oneline) diff --git a/contrib/bash-completion b/contrib/bash-completion index d5a4a1f..31bc6f8 100644 --- a/contrib/bash-completion +++ b/contrib/bash-completion @@ -12,9 +12,9 @@ _hostinfo() { if [[ -z "$arg" ]]; then _completion="$_hostinfo_hosts" else - _completion="hostname os arch addresses ports vserver vserver-host" + _completion="$(hostinfo --keys $arg 2>/dev/null)" fi - COMPREPLY=( $( compgen -W "-o --oneline -f --file $_completion" -- $cur ) ) + COMPREPLY=( $( compgen -W "-o --oneline -f --file -k --keys $_completion" -- $cur ) ) } complete -F _hostinfo hostinfo |