summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Sulfrian <alexander@sulfrian.net>2013-05-16 21:11:27 +0200
committerAlexander Sulfrian <alexander@sulfrian.net>2013-05-16 21:11:27 +0200
commit9a09a2f1a5cd428abc84f615196f518cac3cdc9f (patch)
treecfa83ca803e1597796a56baa34dff0b2e7380440
parentf066a45907943886510fdfe0654be31fcbf758d5 (diff)
downloadtools-9a09a2f1a5cd428abc84f615196f518cac3cdc9f.tar.gz
tools-9a09a2f1a5cd428abc84f615196f518cac3cdc9f.tar.bz2
tools-9a09a2f1a5cd428abc84f615196f518cac3cdc9f.zip
bin/hostinfo: add --file to get the path of the yaml file with the information
-rwxr-xr-xbin/hostinfo68
-rw-r--r--contrib/bash-completion2
2 files changed, 39 insertions, 31 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()
diff --git a/contrib/bash-completion b/contrib/bash-completion
index 7f68819..d5a4a1f 100644
--- a/contrib/bash-completion
+++ b/contrib/bash-completion
@@ -15,6 +15,6 @@ _hostinfo() {
_completion="hostname os arch addresses ports vserver vserver-host"
fi
- COMPREPLY=( $( compgen -W "-o --oneline $_completion" -- $cur ) )
+ COMPREPLY=( $( compgen -W "-o --oneline -f --file $_completion" -- $cur ) )
}
complete -F _hostinfo hostinfo