Skip to content

Instantly share code, notes, and snippets.

@ptone
Created September 14, 2010 21:17
Show Gist options
  • Save ptone/579791 to your computer and use it in GitHub Desktop.
Save ptone/579791 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# encoding: utf-8
"""
Created by Preston Holmes on 2010-09-14.
preston@ptone.com
Copyright (c) 2010
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import sys
import os
import getpass
from subprocess import Popen, call, STDOUT, PIPE
from collections import defaultdict
import re
try:
set
except NameError:
from sets import Set as set
DEBUG = False
def sh(cmd):
return Popen(cmd,shell=True,stdout=PIPE,stderr=PIPE).communicate()[0]
def main():
datasource = "/LDAPv3/127.0.0.1"
diradmin = "diradmin"
diradmin_pass = ""
limit_to_group = ""
if not diradmin_pass:
diradmin_pass = getpass.getpass("password for diradmin:\n")
base_dscl = 'dscl -u %(diradmin)s -P %(diradmin_pass)s %(datasource)s ' % locals()
def dscl(cmd):
# print base_dscl + cmd
return (sh (base_dscl + cmd))
user_list = dscl("list /Users").split('\n')
print "%s users" % len(user_list)
if limit_to_group:
limit_list = dscl("-read /Groups/%s Member" % limit_to_group).split()[1:]
# user_list = [u for u in user_list if u in limit_list]
user_list = list(set(user_list) & set(limit_list))
print "%s users in limit group" % len(user_list)
member_txt = dscl("list /Groups GroupMembership")
# get group membership data structures
group_members = defaultdict(list)
user_groups = defaultdict(list)
for l in member_txt.split('\n'):
words = l.split()
if len(words) < 2:
continue
group = words[0]
group_members[group] = words[1:]
for user in words[1:]:
user_groups[user].append(group)
# get inactive users
print "%s groups" % len(group_members)
inactive_users = []
for u in user_list:
# cmd_out = sh('pwpolicy -n %s -u %s -getpolicy' % (datasource,u))
cmd_out = sh('pwpolicy -u %s -getpolicy' % u)
if re.search("isDisabled=1",cmd_out):
inactive_users.append(u)
print "%s inactive users" % len(inactive_users)
# start the removal
for user in inactive_users:
# remove user from groups
for group in user_groups[user]:
# this only works when running locally
# dseditgroup -n /LDAPv3/fqdn.example.com -u diradmin -P <pass> -o edit -d sstump -t user srstaff2
print "removing %s from %s" % (user,group)
if not DEBUG:
sh("dseditgroup -n %(datasource)s -u %(diradmin)s -P %(diradmin_pass)s -o edit -d %(user)s -t user %(group)s" % locals())
# then delete the user
print "delete " + user
if not DEBUG:
dscl("delete /Users/%s" % user)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment