Skip to content

Instantly share code, notes, and snippets.

@williamcanin
Created September 29, 2015 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save williamcanin/e3768a9398370bb80fa8 to your computer and use it in GitHub Desktop.
Save williamcanin/e3768a9398370bb80fa8 to your computer and use it in GitHub Desktop.
"Modbl" add/remove modules (Linux) to a blacklist.
#!/bin/bash
# --------------------------------------------------------------------------------
# Name: ModBlackList
# Executable: modbl
# Type: Shell
# Description: ModBlackList is a simple script to create a list of blocked modules.
# Credits: William C. Canin <http://williamcanin.com>
# Home page author: http://williamcanin.com
# Province: Brazil/SP
# License: (MIT)
# Usage/URL:https://github.com/williamcanin/modbl
#
BRed='\e[1;31m' # Red
BGreen='\e[1;32m' # Green
BYellow='\e[1;33m' # Yellow
BBlue='\e[1;34m' # Blue
None='\033[00m' # Close
# Variables global
NAME="ModBlackList"
VERSION="1.0.0"
EXEC="modbl"
DIR_BLACKLIST="/etc/modprobe.d"
BLACKLIST_CONF="blacklist.conf"
# Begin Functions
function _ADD_MODULE_BLACKLIST(){
# Create header blacklist in "/etc/modprobe.d".
if [[ ! -f "$DIR_BLACKLIST/$BLACKLIST_CONF" ]]; then
/bin/cat << EOF > $DIR_BLACKLIST/$BLACKLIST_CONF
#
# List of blocked modules, created by $NAME.
#
EOF
fi
SEARCH_MODULE="$(grep -i -n -w "$1" $DIR_BLACKLIST/$BLACKLIST_CONF | cut -d':' -f1)"
if [[ ! -z "$SEARCH_MODULE" ]]; then
printf "\n${BYellow}[ WARNING ] Already exists in this module blacklist.${None}\n\n"
else
# Insert blocked modules.
echo "blacklist $1" >> $DIR_BLACKLIST/$BLACKLIST_CONF
printf "\n${BYellow}Module ($1)${None} ${BGreen}added${None} ${BYellow}to the blacklist.${None}\n${BGreen}Done ✔${None}\n\n"
fi
}
function _REMOVE_MODULE_BLACKLIST(){
SEARCH_MODULE="$(grep -i -n -w "$1" $DIR_BLACKLIST/$BLACKLIST_CONF | cut -d':' -f1)"
if [[ -z "$SEARCH_MODULE" ]]; then
printf "\n${BYellow}[ WARNING ] No module with this name to be removed.${None}\n\n"
else
# Remove module of blacklist
sed -i $SEARCH_MODULE"d" $DIR_BLACKLIST/$BLACKLIST_CONF
printf "\n${BYellow}Module ($1)${None} ${BRed}removed${None} ${BYellow}from the blacklist.${None}\n${BGreen}Done ✔${None}\n\n"
fi
}
function _HELP(){
printf "\nUsage: $0 { -a <module_name> | -r <module_name> | uninstall | help } \n"
printf "\n======================================================"
printf "\n Command Description\n"
printf "======================================================"
printf "\n\n-a (--add) Add module in the blacklist\n"
printf "\n\n-r (--remove) Remove module in the blacklist\n"
printf "\n\nuninstall Uninstall $NAME\n\n"
printf "\n\n$NAME version $VERSION (c) Copyright $(date +%Y)\n\n"
}
function _UNINSTALL(){
if [[ ! -f "$DIR_BLACKLIST/$BLACKLIST_CONF" ]]; then
rm -f /usr/bin/$EXEC
printf "\n${BGreen}Complete uninstall ✔${None}\n\n"
else
printf "\nYou are uninstalling the $NAME.\nWant to delete the blacklist modules?(y/n)\n\n"
printf "Reply > "
read resp_uninstall
case "$resp_uninstall" in
y|Y)
rm -f /usr/bin/$EXEC
rm -f $DIR_BLACKLIST/$BLACKLIST_CONF
printf "\n${BGreen}Complete uninstall ✔${None}\n\n"
;;
n|N)
rm -f /usr/bin/$EXEC
printf "\n${BGreen}Complete uninstall ✔${None}\n\n"
printf "\n${BYellow}[ WARNING ] The blacklist was maintained.${None}\n\n"
;;
esac
fi
}
# End Functions
# Main
if [[ "$(id -u)" != "0" ]]; then
printf "\n${BRed}[x] You need to be root to use the $NAME (or sudo).${None}\n\n"
else
case "$1" in
-a|--add)
if [[ -z "$2" ]]; then
bash $0
else
_ADD_MODULE_BLACKLIST $2
fi
;;
-r|--remove)
if [[ -z "$2" ]]; then
bash $0
else
_REMOVE_MODULE_BLACKLIST $2
fi
;;
uninstall)
_UNINSTALL
;;
help)
_HELP
;;
*)
printf "\n${BYellow}[ WARNING ] You must pass the module as a parameter to put on blacklist.${None}\n"
printf "\nUsage: $0 { -a <module_name> | -r <module_name> | uninstall | help } \n"
printf "\n\n\n$NAME version $VERSION (c) Copyright $(date +%Y)\n\n"
;;
esac
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment