Skip to content

Instantly share code, notes, and snippets.

@av1d
Last active August 12, 2021 16:26
Show Gist options
  • Save av1d/ea8f9b1ddd6bb35e742ddea7af16c041 to your computer and use it in GitHub Desktop.
Save av1d/ea8f9b1ddd6bb35e742ddea7af16c041 to your computer and use it in GitHub Desktop.
Warn user when they issue sudo rm -rf, passes all other sudo commands otherwise
#!/usr/bin/env python
import os
import sys
# Warns user before issuing deadly commands with sudo.
#
# add alias to ~/.bashrc like so:
# alias sudo="/home/users/sudo_rm.py"
# then reload bashrc:
# source ~/.bashrc
class alertColor:
OKGREEN = '\033[92m'
WARNYEL = '\033[93m'
WARNRED = '\033[91m'
ENDCOLOR = '\033[0m'
if sys.argv[1] == "rm":
if sys.argv[2] == "-rf":
print(alertColor.WARNYEL + "WARNING: you are about to execute " + alertColor.OKGREEN + "rm -rf" + alertColor.WARNYEL + " as sudo!" + alertColor.ENDCOLOR)
warnprompt = raw_input(alertColor.WARNYEL + "ARE YOU SURE you want to " + str(" ".join(sys.argv[1:])) + "(y/n) ??? " + alertColor.ENDCOLOR)
if warnprompt == "y":
cmd = "sudo " + (" ".join(sys.argv[1:]))
os.system(cmd)
sys.exit(1)
else:
print("Not executing command...")
sys.exit(1)
cmd = "sudo " + (" ".join(sys.argv[1:]))
os.system(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment