summaryrefslogtreecommitdiffstats
path: root/scripts/selinux-info
blob: a8d5e765da1097b8adcfe2392e6491e0454d04e4 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python -tt
__doc__ = doc = """
print out the selinux status of hosts
    by ralph
    gplv2+
    derived from vhost-info by skvidal
""".strip()


import pprint
import sys
import ansible.runner
import os

from argparse import ArgumentParser


def parse_args(args):
    parser = ArgumentParser(doc)
    parser.add_argument('--host', default=[], action='append',
               help="hosts to act on, defaults to ALL")
    parser.add_argument('--timeout', default=30, type=int,
               help='set the wait timeout for func commands')
    parser.add_argument('--hosts-from-file', default=None, dest="hostfile",
               help="read list of hosts from this file, if '-' read from stdin")
    args = parser.parse_args(args)

    if args.hostfile:
        hosts = []
        if args.hostfile == '-':
            hosts = sys.stdin.readlines()
        else:
            hosts = open(args.hostfile, 'r').readlines()

        for hn in hosts:
            hn = hn.strip()
            if hn.startswith('#'):
                continue
            hn = hn.replace('\n', '')
            args.host.append(hn)

    if not args.host:
        args.host = ["all"]

    return args, parser


args, parser = parse_args(sys.argv[1:])
hosts ='*'
if args.host:
    hosts = ';'.join(args.host)

if os.geteuid() == 0:
    login = 'root'
else:
    login = os.getlogin()


results, errors = {}, []

ansible_args = dict(
    pattern=hosts,
    module_name='command',
    module_args='getenforce',
    forks=25,
    transport='paramiko',
    timeout=10,
    remote_user=login,
)

ans = ansible.runner.Runner(**ansible_args)
res = ans.run()

for hn in sorted(res['contacted']):
    if 'failed' in res['contacted'][hn] and res['contacted'][hn]['failed']:
        errors.append(hn)
        continue
    status = res['contacted'][hn]['stdout']
    results[status] = results.get(status, []) + [hn]

pprint.pprint(results)

if errors:
    print "ERRORED:", pprint.pformat(errors)