Last active
June 7, 2023 18:43
-
-
Save prehensilecode/3ff027aea10566726ec5a615fab5fddd to your computer and use it in GitHub Desktop.
Find nonexistent users in a sudoers file
This file contains hidden or 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 python3 | |
| import sys | |
| import os | |
| import re | |
| import pwd | |
| # some of the User_Aliases lines contain buggy usernames | |
| # which have "a" at the end of the username | |
| broken_usernames = set() | |
| usernames = set() | |
| useralias_pat = re.compile(r'^User_Alias') | |
| with open('sudoers', 'r') as sudoersfile: | |
| for l in sudoersfile: | |
| if useralias_pat.match(l): | |
| usernames.update(l.strip().split('=')[-1].strip().split(',')) | |
| for u in usernames: | |
| try: | |
| pwd.getpwnam(u) | |
| except Exception as e: | |
| broken_usernames.add(u) | |
| print('Broken usernames:') | |
| for u in sorted(broken_usernames): | |
| print('\t', u) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment