Skip to content

Instantly share code, notes, and snippets.

@asifiqbal
Created November 11, 2017 04:45
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 asifiqbal/174d4ca30b0afa473dff1b8ee57f400d to your computer and use it in GitHub Desktop.
Save asifiqbal/174d4ca30b0afa473dff1b8ee57f400d to your computer and use it in GitHub Desktop.
parse groups
#!/usr/bin/env python
with open('/etc/group') as f:
groups = f.readlines()
mygroups = {}
for ll in groups:
ll = ll.split(':')
if len(ll[3]) > 1:
groupname, groupmembers = ll[0], ll[3].strip()
if groupname != groupmembers:
mygroups[groupname] = [ x for x in groupmembers.split(',') ]
for key,values in mygroups.items():
print "groupadd {}".format(key)
for v in values:
print "usermod -aG {} {}".format(key,v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment