#!/bin/bash ############################################################ # Decription: Template script # Author: Gilles Mouchet (gilles.mouchet@gmail.com) # Creation Date: 06-Sep-2025 # Version: 1.0.0 # # Changelog: # V1.0.0 - 25-Sep-2025 - GMo # Added # - Creation of script from scratch # ############################################################ #----------------------------------------------------------------- # DON'T CHANGE ANYTHING FROM HERE #----------------------------------------------------------------- version="1.0.0" mailSubject="[SUCCESS] - script result on `hostname`" mailHeader="my_hdr From: GMO Check System " mailBody="" mailFooter="\n\nTemplate script $version by Exploit GMoTech" tmpFile=/tmp/list.txt #----------------------------------------------------------- # FUNCTIONS #----------------------------------------------------------- function usage() { cat << EOF Usage: ./$(basename "$0") options Template script Options: -p, --param - display parameters -h, --help - display this help -v, --version - display script version EOF } #----------------------------------------------------------- function sendMail() { if [ -f "$tmpFile" ];then echo -e "$mailBody $mailFooter" | mutt -e "$mailHeader" -s "${mailSubject}" $msgRecipient -a $tmpFile else echo -e "$mailBody $mailFooter" | mutt -e "$mailHeader" -s "${mailSubject}" $msgRecipient fi } #----------------------------------------------------------- # Function write log in file log # parameter # $1 define entry type (info, warning, error) # $2 define text # $3 define display on screen or not (nothing=no, 1=yes) function log() { if [ -z "$3" ]; then displayScreen=0 else displayScreen=1 fi case "$1" in I) logType="[info]" ;; W) logType="[warning]" ;; E) logType="[error]" ;; esac # on screen and logfile #echo "$(date "+%Y-%m-%d")-$(date "+%H:%M:%S") - $logType - $2" | tee -a "$logFile" #echo "[$(date "+%Y-%m-%d")-$(date "+%H:%M:%S")] - $logType - $2" >> "$logFile" # true to display screen to if [ "${displayScreen}" -eq 1 ];then echo "[$(date "+%Y-%m-%d")-$(date "+%H:%M:%S")] - $logType - $2" | tee -a "$logFile" else echo "[$(date "+%Y-%m-%d")-$(date "+%H:%M:%S")] - $logType - $2" >> "$logFile" fi } #----------------------------------------------------------- # MAIN #----------------------------------------------------------- # check if the effective user ID is 0 (root) if [[ $EUID -ne 0 ]]; then echo "This script must be run as root or with sudo." exit 1 fi # config progName=`echo $0 | sed -e 's|.*/||g' | cut -f1 -d.` confDir=/etc/$progName cfgFile=$confDir/$progName.conf logPath=/var/log/$progName logFile=$logPath/$progName.log # check if conf file or passphrase file exist if [ ! -f $cfgFile ]; then echo "$progName not installed correctly. Please run install.sh script" exit 1 fi # read config file . $cfgFile log I "script start" 1 # check if logPath exist if [ ! -d $logPath ]; then mkdir $logPath fi # check param exist. Uncomment if your script need parameters #if [ -z "$1" ]; then # usage # exit #fi while [[ "$#" -gt 0 ]]; do case "$1" in -p|--param) cat << EOF ------------------------------------------------------------------------------- Parameters ------------------------------------------------------------------------------- Defined in script ------------------------------------------------------------------------------- script name: $progName config folder: $confDir config file: $cfgFile log path: $logPath log file: $logFile ------------------------------------------------------------------------------- Defined in $cfgFile ------------------------------------------------------------------------------- message recipient: $msgRecipient EOF exit ;; version|-v|--version) cat << EOF $(basename "$0") v$version (c) 1990 - $(date +%Y) by Gilles Mouchet Non-Commercial Use License – See LICENSE for details EOF exit ;; # must be in the last block of the case because of * *|help|-h|--help) usage exit ;; esac shift done # success message log I "send a success message" 1 mailSubject="[SUCCESS] - script result on `hostname`" mailBody=" This is a success test mail\nHave a good day" sendMail # warnig message log W "send a warning message" 1 mailSubject="[WARNING] - script result on `hostname`" mailBody=" This is a warning test mail\nHave a good day" sendMail # error message log E "send an error message" 1 cat << EOF > $tmpFile This file contain the description error or log file EOF mailSubject="[ERROR] - script result on `hostname`" mailBody=" This is a warning test mail\nHave a good day" sendMail rm -rf $tmpFile