diff options
author | Alexander Sulfrian <alexander@sulfrian.net> | 2013-05-06 17:48:16 +0200 |
---|---|---|
committer | Alexander Sulfrian <alexander@sulfrian.net> | 2013-05-06 17:48:16 +0200 |
commit | 0e2c6645d29b1b943dcd97ada15523633a8d23e9 (patch) | |
tree | 7e81e34118cee50436311b413d6e456bf0b849f0 /bin | |
parent | 25f54ac07dcc62d44059fd8f73091a1b3a535fa3 (diff) | |
download | tools-0e2c6645d29b1b943dcd97ada15523633a8d23e9.tar.gz tools-0e2c6645d29b1b943dcd97ada15523633a8d23e9.tar.bz2 tools-0e2c6645d29b1b943dcd97ada15523633a8d23e9.zip |
add setup.py0.1.1
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/hostinfo | 59 |
1 files changed, 59 insertions, 0 deletions
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 <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() |