From 0e2c6645d29b1b943dcd97ada15523633a8d23e9 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Mon, 6 May 2013 17:48:16 +0200 Subject: add setup.py --- bin/hostinfo | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 bin/hostinfo (limited to 'bin') diff --git a/bin/hostinfo b/bin/hostinfo new file mode 100755 index 0000000..25c7509 --- /dev/null +++ b/bin/hostinfo @@ -0,0 +1,59 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import sys +import socket +import yaml +import os.path +from dns import resolver,reversename + +import hostinfo.printer + +def print_info(path, key=None): + stream = file(path, 'r') + data = yaml.load(stream) + + p = printer.Printer(data) + p.info(key) + +def find_host(host, key=None): + path = "/usr/local/share/hostinfo/%s" % host + + if not os.path.exists(path): + # try to build the fqdn + path = "/usr/local/share/hostinfo/%s.spline.inf.fu-berlin.de" % \ + host.replace('.spline.de', '') + + if not os.path.exists(path): + try: + # try to use reverse dns + addr=reversename.from_address(host) + hostname = str(resolver.query(addr,"PTR")[0]) + path = "/usr/local/share/hostinfo/%s" % hostname[0:-1] + except: + pass + + if not os.path.exists(path): + return False + + print_info(path, key) + return True + +def main(): + if len(sys.argv) < 2: + print('Usage: %s [info]' % sys.argv[0]) + sys.exit(1) + + if len(sys.argv) == 2: + if find_host(sys.argv[1]): + sys.exit(0) + else: + if find_host(sys.argv[1], sys.argv[2]): + sys.exit(0) + + print("Host '%s' could not be found!" % sys.argv[1]) + sys.exit(1) + + +if __name__ == '__main__': + main() -- cgit v1.2.3-1-g7c22