Created
February 5, 2013 17:57
-
-
Save adamcrown/4716286 to your computer and use it in GitHub Desktop.
A couple simple classes to aid in batch searching and deleting users from a local Apple Open Directory server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LocalODServer | |
attr_accessor :username, :password | |
def initialize(username, password) | |
@username = username | |
@password = password | |
end | |
def ip | |
'127.0.0.1' | |
end | |
def get_users(attribute, value) | |
dscl.search_users attribute, value | |
end | |
def delete_user(username) | |
dscl.delete_user username | |
end | |
private | |
def dscl | |
@dscl ||= DSCL.new self | |
end | |
end | |
class DSCL | |
attr_accessor :od_server | |
def initialize(od_server) | |
@od_server = od_server | |
end | |
def exec(command) | |
`dscl -u #{@od_server.username} -P #{@od_server.password} /LDAPv3/#{@od_server.ip} #{command}` | |
end | |
def search(path, attribute, value) | |
self.exec "-search #{path} #{attribute} #{value}" | |
end | |
def search_users(attribute, value) | |
results_to_usernames search("/Users", attribute, value) | |
end | |
def delete_user(username) | |
self.exec "-delete /Users/#{username}" | |
end | |
private | |
def results_to_usernames(results) | |
results.scan(/^([a-z]+[0-9]*)\t{2}/).flatten | |
end | |
end | |
# Example usage | |
# od = LocalODServer.new 'admin', 'secret' | |
# usernames = od.get_users('eduPersonPrimaryAffiliation', 'alumnus') | |
# usernames.each { |username| od.delete_user username } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment