summaryrefslogtreecommitdiffstats
path: root/hostinfo
diff options
context:
space:
mode:
Diffstat (limited to 'hostinfo')
-rwxr-xr-xhostinfo59
-rw-r--r--hostinfo/__init__.py0
-rw-r--r--hostinfo/printer.py74
3 files changed, 74 insertions, 59 deletions
diff --git a/hostinfo b/hostinfo
deleted file mode 100755
index bf08d03..0000000
--- a/hostinfo
+++ /dev/null
@@ -1,59 +0,0 @@
-#!/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()
diff --git a/hostinfo/__init__.py b/hostinfo/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/hostinfo/__init__.py
diff --git a/hostinfo/printer.py b/hostinfo/printer.py
new file mode 100644
index 0000000..7d440d0
--- /dev/null
+++ b/hostinfo/printer.py
@@ -0,0 +1,74 @@
+# -*- coding: utf-8 -*-
+
+class Printer:
+ labels = {
+ 'hostname': 'Hostname',
+ 'arch': 'Architecture',
+ 'os': 'Operating System',
+ 'addresses': 'IPs'
+ }
+
+ sort = ['hostname', 'arch', 'os', 'addresses']
+ ignore = ['vserver-host']
+
+ def __init__(self, data):
+ self.data = data
+
+ def print_addresses(self, label, value):
+ for ip in value:
+ print('%s%s/%s (%s)' %
+ (label, ip['address'], ip['netmask'], ip['interface']))
+ label = self.empty
+
+ def print_vserver(self, label, value):
+ if value == 'guest':
+ print('%sguest running on %s' %
+ (label, self.data['vserver-host']))
+ else:
+ print('%s %s' % (self.label, value))
+
+ def print_default(self, key, value):
+ label = ''
+ if self.length > 0:
+ label = '%s: ' % self.get_label(key)
+ label = label.rjust(self.length)
+
+ try:
+ method = getattr(self, 'print_%s' % key)
+ method(label, value)
+ except AttributeError:
+ if isinstance(value, list):
+ for v in value:
+ print('%s%s' % (label, v))
+ label = self.empty
+ else:
+ print('%s%s' % (label, value))
+
+ def get_label(self, key):
+ if key in self.labels:
+ return self.labels[key]
+ return key
+
+ def info(self, key):
+ # find max lenght for labels
+ labels = map(self.get_label, self.data.keys())
+ self.length = max(map(len, labels)) + 3
+ self.empty = ' ' * self.length
+
+ if key is not None:
+ self.length = 0
+ self.empty = ''
+ if key in self.data:
+ self.print_default(key, self.data[key])
+ else:
+ # first the sorted keys
+ for key in self.sort:
+ if key in self.data:
+ self.print_default(key, self.data[key])
+
+ # other keys, that are not ignored
+ for key in [k for k in self.data.keys()
+ if k not in self.sort
+ and k not in self.ignore]:
+ self.print_default(key, self.data[key])
+