Skip to content

Instantly share code, notes, and snippets.

@esammer
Created July 24, 2012 19:06
Show Gist options
  • Save esammer/3171934 to your computer and use it in GitHub Desktop.
Save esammer/3171934 to your computer and use it in GitHub Desktop.
Load two /etc/passwd files and find all users and groups that need to be fixed on a machine.
#!/usr/bin/python
import sys
def load_passwd(path):
lookup = { }
f = file(path)
for line in f:
line = line.rstrip()
fields = line.split(':')
#!/usr/bin/python
import sys
def load_passwd(path):
lookup = { }
f = file(path)
for line in f:
line = line.rstrip()
fields = line.split(':')
lookup[fields[0]] = { 'uid': fields[2], 'gid': fields[3] }
f.close()
return lookup
if __name__ == '__main__':
if len(sys.argv) != 3:
print "Usage: %s <current passwd> <new passwd>" % (sys.argv[0])
sys.exit(1)
current_passwd = load_passwd(sys.argv[1])
new_passwd = load_passwd(sys.argv[2])
for user in new_passwd.keys():
if current_passwd.has_key(user):
current = current_passwd[user]
new = new_passwd[user]
if current['uid'] != new['uid'] or current['gid'] != new['gid']:
print "Need to change %s (uid:%s gid:%s) -> (uid:%s gid:%s)" % \
(user, current['uid'], current['gid'], new['uid'], new['gid'])
print " find / \( -uid %s -exec chown %s \{\} \; \) -o \( -gid %s -exec chgrp %s \{\} \; \)" % \
(current['uid'], new['uid'], current['gid'], new['gid'])
else:
print "Need to add %s (uid:%s gid:%s)" % (user, new['uid'], new['gid'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment