Skip to content

Instantly share code, notes, and snippets.

@xr09
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save xr09/7030e6b5ff059fa5aef6 to your computer and use it in GitHub Desktop.

Select an option

Save xr09/7030e6b5ff059fa5aef6 to your computer and use it in GitHub Desktop.
the home nuker
import os
import stat
import shutil
import subprocess
# protected folders in /home
DONT_DELETE = 'Department lost+found someguy'.split()
def chmod_writable(func, path, excinfo):
""" chmod +w file """
os.chmod(path, stat.S_IWRITE)
func(path)
def nukedir(path):
""" Recursive delete (rm -rf) """
print "Nuking: %s" % path
shutil.rmtree(path, onerror=chmod_writable)
def get_user_list():
"""
Returns a list with current logged users and protected folders
"""
command = subprocess.Popen( ["-c", "who | cut -d' ' -f1 | sort | uniq | grep -v root" ], stdout=subprocess.PIPE, shell=True )
users, err = command.communicate()
if users:
users = [x for x in users.split('\n') if x]
else:
users = []
return set(users).union(set(DONT_DELETE))
if __name__ == "__main__":
protected_users = get_user_list()
path = '/home'
for i in next(os.walk(path))[1]:
if i not in protected_users:
nukedir(os.path.join(path, i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment