1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import socket
import getopt
import yaml
import os.path
from dns import resolver,reversename
own_directory = os.path.dirname(os.path.abspath(__file__))
lib = os.path.join(own_directory, '..')
if os.path.exists(os.path.join(lib, 'hostinfo')):
sys.path = [lib] + sys.path
from hostinfo import printer
def print_info(path, key=None, oneline=False):
stream = file(path, 'r')
data = yaml.load(stream)
p = printer.Printer(data, oneline)
p.info(key)
def find_host(host, key=None, oneline=False):
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, oneline)
return True
def main():
self_name = sys.argv.pop(0)
optlist, args = getopt.gnu_getopt(sys.argv, 'o', ['oneline'])
flags = [opt for (opt, _) in optlist]
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)
if __name__ == '__main__':
main()
|