#!/usr/bin/env python # -*- coding: utf-8 -*- import sys import socket import getopt import yaml import os from dns import resolver,reversename own_directory = os.path.dirname(os.path.abspath(os.path.realpath(__file__))) lib = os.path.join(own_directory, '..') if os.path.exists(os.path.join(lib, 'hostinfo')): sys.path = [lib] + sys.path from hostinfo import printer def _get_data(path): stream = file(path, 'r') return yaml.load(stream) def print_info(path, key=None, flags=list()): data = _get_data(path) p = printer.Printer(data, flags) p.info(key) def print_keys(path): def _print_keys(data, prefix = ''): if isinstance(data, str): return 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) elif 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') elif isinstance(data[key], dict): _print_keys(data[key], "%s%s." % (prefix, key)) elif isinstance(data[key], list): for v in data[key]: _print_keys(v, "%s%s." % (prefix, key)) data = _get_data(path) _print_keys(data) def print_hosts(path, short): metadata = os.path.join(path, 'metadata', 'hosts') if os.path.exists(metadata): hosts = yaml.load(file(metadata, 'r')) if 'hosts' in hosts: for host in hosts['hosts']: if short: print(host.replace('.spline.inf.fu-berlin.de','')) else: print(host) return True sys.stderr.write("'%s' not found!\n" % metadata) return False def find_host(basepath, host): path = os.path.join(basepath, host) if os.path.exists(path): return path # try to build the fqdn path = os.path.join(basepath, "%s.spline.inf.fu-berlin.de" % host.replace('.spline.de', '')) if os.path.exists(path): return path try: # try to use reverse dns addr=reversename.from_address(host) hostname = str(resolver.query(addr,"PTR")[0]) path = os.path.join(basepath, hostname[0:-1]) except: pass if os.path.exists(path): return path return None def print_help(self_name): print('Usage: %s [info]' % self_name) def main(): self_name = sys.argv.pop(0) basepath = '/usr/local/share/hostinfo' if 'HOSTINFO_PATH' in os.environ and os.environ['HOSTINFO_PATH'] != '': basepath = os.environ['HOSTINFO_PATH'] file = keys = help = hosts = short = False flags = list() try: optlist, args = getopt.gnu_getopt(sys.argv, 'ofkvnh?p:ls', ['oneline', 'file', 'keys', 'verbose', 'nospaces', 'help', 'path=', 'hosts', 'short']) opts = {} for key, value in optlist: opts[key] = value file = any([o in opts for o in ['--file', '-f']]) keys = any([o in opts for o in ['--keys', '-k']]) help = any([o in opts for o in ['--help', '-h', '-?']]) hosts = any([o in opts for o in ['--hosts', '-l']]) short = any([o in opts for o in ['--short', '-s']]) if any([o in opts for o in ['--verbose', '-v']]): flags.append('verbose') if any([o in opts for o in ['--oneline', '-o']]): flags.append('oneline') if any([o in opts for o in ['--nospaces', '-n']]): flags.append('nospaces') if any([o in opts for o in ['--path', '-p']]): basepath = [opts[o] for o in ['--path', '-p'] if o in opts][0] except getopt.GetoptError, e: sys.stderr.write("Error: %s\n\n" % e) args = [] if hosts: if not print_hosts(basepath, short): sys.exit(1) sys.exit(0) if help or len(args) < 1: print_help(self_name) if not help: sys.exit(1) sys.exit(0) path = find_host(basepath, args[0]) if path is None: sys.stderr.write("Host '%s' could not be found!\n" % args[0]) sys.exit(1) if file: print(path) elif keys: print_keys(path) else: key=None if len(args) > 1: key = args[1] print_info(path, key=key, flags=flags) sys.exit(0) if __name__ == '__main__': main()