diff options
Diffstat (limited to 'hostinfo')
-rwxr-xr-x | hostinfo | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/hostinfo b/hostinfo new file mode 100755 index 0000000..bf08d03 --- /dev/null +++ b/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 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 <host> [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() |