Skip to content

Instantly share code, notes, and snippets.

@nsabine
Last active December 16, 2015 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nsabine/5483003 to your computer and use it in GitHub Desktop.
Save nsabine/5483003 to your computer and use it in GitHub Desktop.
Find duplicate user ids on a system by parsing the /etc/passwd file
#!/usr/bin/env python
from collections import defaultdict
# Initialize dictionary of user ids
uids = defaultdict(list)
# loop through password file, building dictionary of uid:[list of usernames]
with open("/etc/passwd") as passwd_file:
for line in passwd_file:
line_array = line.split(":")
uids[line_array[2]].append(line_array[0])
# loop though dictionary.
# If duplicate usernames for uid found, print on standard out
for uid in uids:
if len(uids[uid]) > 1:
print ( uid + ": " + " ".join(uids[uid]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment