bashscripts/init_scripts.sh

112 lines
3.6 KiB
Bash

#!/bin/bash
# Functions
show_cron_help () {
echo "--------------------------------------------"
echo "* * * * * command to be executed"
echo "- - - - -"
echo "| | | | |"
echo "| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)"
echo "| | | ------- Month (1 - 12)"
echo "| | --------- Day of month (1 - 31)"
echo "| ----------- Hour (0 - 23)"
echo "------------- Minute (0 - 59)"
echo "Examples:"
echo "(predefined) @annually | @yearly | @monthly | @weekly | @daily | @hourly | @reboot"
echo "(every) @every 5s | @every 20h30m"
echo "(custom) 10 20 * * * | 0 3 * * 1-5"
echo -e "--------------------------------------------\n"
}
get_cron () {
cron=false
while [ "$cron" != "true" ]
do
read -p 'Escriu el periode del cron: ' CRON
case $CRON in
("") echo "El cron no pot ser buit";;
(*) cron=true #echo "just numeric";;
esac
done
echo "$CRON"
}
# End functions
readonly SCRIPTS_FOLDER="e7scripts"
readonly GOTIFY_SCRIPT_NAME="gotifypush.sh"
readonly GOTIFY_SCRIPT_URL="https://git.studi7.com/roger/bashscripts/raw/branch/master/gotifypush.sh"
readonly UPDATES_SCRIPT_NAME="updates_notifier.sh"
readonly UPDATES_SCRIPT_URL="https://git.studi7.com/roger/bashscripts/raw/branch/master/updates_notifier.sh"
#verify run as root or sudo
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
#set -f
#func_result="$(get_cron)"
#echo $func_result
#set +f
echo "/////////////////////////////////////////"
echo "Benvingut a l'instalador d'Scripts"
echo -e "/////////////////////////////////////\n"
finished=false
while [ "$finished" != "true" ]
do
# Choose script to install
echo "0) Sortir de l'instalador"
echo "1) Notificador dels resultats dels scripts (Gotify Push Notifications)"
echo "2) Notificador d'actualitzacions"
echo "3) Escaneig de rootkits amb RK Hunter"
echo "4) Escaneig de rootkits amb CHK Rootkit"
echo -e "5) Escaneig de virus amb ClamAV\n"
choosed=false
while [ "$choosed" != "true" ]
do
read -p 'Indica quin script vols instal·lar (0 per sortir): ' SCRIPT_NUM
case $SCRIPT_NUM in
("") echo "El número no pot ser buit";;
(*[!0-9]*) echo "Has d'introduir un número";;
(*) choosed=true #echo "just numeric";;
esac
done
if [ "$SCRIPT_NUM" = "0" ]; then
finished=true
exit 0
elif [ "$SCRIPT_NUM" = "1" ]; then
mkdir -p $SCRIPTS_FOLDER
wget -O "$SCRIPTS_FOLDER/$GOTIFY_SCRIPT_NAME" $GOTIFY_SCRIPT_URL
read -p 'Indica el domini de gotify: ' GOTIFY_DOMAIN
sed -i "s/push.example.com/$GOTIFY_DOMAIN/" "$SCRIPTS_FOLDER/$GOTIFY_SCRIPT_NAME"
chmod +x "$SCRIPTS_FOLDER/$GOTIFY_SCRIPT_NAME"
echo -e "Script instal·lat correctament\n"
elif [ "$SCRIPT_NUM" = "2" ];then
mkdir -p $SCRIPTS_FOLDER
wget -O "$SCRIPTS_FOLDER/$UPDATES_SCRIPT_NAME" $UPDATES_SCRIPT_URL
read -p 'Indica el token del canal de gotify: ' TOKEN
sed -i "s/<TOKEN_GOTIFY_APP>/$TOKEN/" "$SCRIPTS_FOLDER/$UPDATES_SCRIPT_NAME"
sed -i "s/Hostname/$(hostname)/" "$SCRIPTS_FOLDER/$UPDATES_SCRIPT_NAME"
chmod +x "$SCRIPTS_FOLDER/$UPDATES_SCRIPT_NAME"
echo -e "\nAFEGEIX EL CRON\n"
show_cron_help
cron="$(get_cron)"
#https://stackoverflow.com/questions/878600/how-to-create-a-cron-job-using-bash-automatically-without-the-interactive-editor
#remove previous crontab
(crontab -l | grep -v -F "$UPDATES_SCRIPT_NAME") | crontab -
#add crontab
(crontab -l ; echo "$cron cd $(pwd)/$SCRIPTS_FOLDER/ && ./$UPDATES_SCRIPT_NAME 2>&1") | crontab -
echo -e "Script instal·lat correctament\n"
#TODO Function cron get period
else
echo -e "Aquest número d'script no existeix. Tornar a intentar-ho\n"
fi
done