Skip to content

Instantly share code, notes, and snippets.

@arubdesu
Last active October 3, 2017 03:19
Show Gist options
  • Save arubdesu/98231ad6a7575b3239ce to your computer and use it in GitHub Desktop.
Save arubdesu/98231ad6a7575b3239ce to your computer and use it in GitHub Desktop.
dscl+python for common tasks
#!/usr/bin/python
import subprocess
import sys
from SystemConfiguration import *
# grab who our logging in user is
cur_console_user = SCDynamicStoreCopyConsoleUser(None, None, None)[0]
# check admin group
cmd = '/usr/bin/dscl', '.', 'list', '/Groups/admin'
out = subprocess.check_output(cmd)
# split output into list of strings for the usernames
alladmins = (out).split()
# whitelist
allowed_and_syss = ['jssadmin', 'root', 'puppet', 'daemon', 'nobody', 'Guest', 'jenkins', 'ourOrgsAdmin']
# check if our console user is not in our whitelist
if cur_console_user not in allowed_and_syss:
# catch error just in case, nothing special
try:
cmd = '/usr/bin/dscl', '.', 'delete', '/Groups/admin', user
out = subprocess.check_output(cmd)
print 'Done did removed %s', user
sys.exit(0)
except Exception as e:
raise e
else:
print 'Nobody to remove, all good'
sys.exit(0)
#!/usr/bin/python
import subprocess
cmd = '/usr/bin/dscl', '.', 'list', '/Users'
task = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = task.communicate()
allusers = (out).split()
nonsyss = []
syss = ['jssadmin', 'root', 'puppet', 'daemon', 'nobody', 'Guest', 'jenkins']
for user in allusers:
if (not user[0] == '_') and (user not in syss):
nonsyss.append(user)
print nonsyss
@YehudaBialik
Copy link

Thanks for the help. I ended up using bash instead.

https://gist.github.com/3a8cf25e91fa01cf3ee3.git

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment