summaryrefslogtreecommitdiffstats
path: root/bin/hostinfo
diff options
context:
space:
mode:
Diffstat (limited to 'bin/hostinfo')
-rwxr-xr-xbin/hostinfo68
1 files changed, 38 insertions, 30 deletions
diff --git a/bin/hostinfo b/bin/hostinfo
index 1ee31d5..1b92fdf 100755
--- a/bin/hostinfo
+++ b/bin/hostinfo
@@ -15,57 +15,65 @@ if os.path.exists(os.path.join(lib, 'hostinfo')):
from hostinfo import printer
-def print_info(path, key=None, oneline=False):
+def _get_data(path):
stream = file(path, 'r')
- data = yaml.load(stream)
+ return yaml.load(stream)
+def print_info(path, key=None, oneline=False):
+ data = _get_data(path)
p = printer.Printer(data, oneline)
p.info(key)
-def find_host(host, key=None, oneline=False):
+def find_host(host):
path = "/usr/local/share/hostinfo/%s" % host
+ if os.path.exists(path):
+ return path
- 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', '')
+ # try to build the fqdn
+ path = "/usr/local/share/hostinfo/%s.spline.inf.fu-berlin.de" % \
+ host.replace('.spline.de', '')
+ if os.path.exists(path):
+ return path
- 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
+ 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
+ if os.path.exists(path):
+ return path
- print_info(path, key, oneline)
- return True
+ return None
def main():
self_name = sys.argv.pop(0)
- optlist, args = getopt.gnu_getopt(sys.argv, 'o', ['oneline'])
- flags = [opt for (opt, _) in optlist]
+ optlist, args = getopt.gnu_getopt(sys.argv, 'of', ['oneline', 'file'])
+ 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
if len(args) < 1:
print('Usage: %s <host> [info]' % self_name)
sys.exit(1)
- if len(args) == 1:
- if find_host(args[0], oneline=oneline):
- sys.exit(0)
- else:
- if find_host(args[0], args[1], oneline=oneline):
- sys.exit(0)
-
- print("Host '%s' could not be found!" % args[0])
- sys.exit(1)
+ path = find_host(args[0])
+ if path is None:
+ print("Host '%s' could not be found!" % args[0])
+ sys.exit(1)
+ if file:
+ print(path)
+ else:
+ if len(args) == 1:
+ print_info(path, oneline=oneline)
+ else:
+ print_info(path, key=args[1], oneline=oneline)
+ sys.exit(0)
if __name__ == '__main__':
main()