Last active
April 26, 2018 02:27
-
-
Save robbyt/389823e1c7028b1da3b629f61e49d183 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import json | |
import os | |
import re | |
from glob import glob | |
SUDOERS_D = '/etc/sudoers.d' | |
SUDOERS_FILE = '/etc/sudoers' | |
__doc__ = "Prints all sudoers entries (users/groups) found in /etc/sudoers.d & /etc/sudoers, and their assigned rules to stdout" | |
def main(): | |
sudoers = [] | |
if os.path.isdir(SUDOERS_D): | |
for sudoers_file_path in glob(SUDOERS_D + '/*'): | |
f = open(sudoers_file_path, 'r') | |
for li in f.readlines(): | |
li = li.rstrip() # trim trailing whitespace | |
match = re.match(r"^(\S*)\s*(.*)", li) # match "(username/group)space(rule)" | |
if match: | |
sudoers.append(match.groups()) | |
f.close() | |
sudoers_file = open(SUDOERS_FILE, 'r') | |
for li in sudoers_file.readlines(): | |
li = li.strip() | |
if li.startswith('Defaults') or li.startswith('#') or li == "": | |
continue | |
match = re.match(r"^(\S*)\s*(.*)", li) | |
if match: | |
sudoers.append(match.groups()) | |
sudoers_file.close() | |
sudoers.sort(key=lambda x: x[0]) | |
return json.dumps(sudoers, sort_keys=True, indent=4, separators=(',', ': ')) | |
if __name__ == '__main__': | |
print(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment