Skip to content

Instantly share code, notes, and snippets.

@prehensilecode
Last active June 7, 2023 18:43
Show Gist options
  • Save prehensilecode/3ff027aea10566726ec5a615fab5fddd to your computer and use it in GitHub Desktop.
Save prehensilecode/3ff027aea10566726ec5a615fab5fddd to your computer and use it in GitHub Desktop.
Find nonexistent users in a sudoers file
#!/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