From e379ecef0fdba749c12939101209ae7775311430 Mon Sep 17 00:00:00 2001 From: scripts <> Date: Sat, 6 Sep 2025 08:53:35 +0200 Subject: [PATCH] Initial commit --- .vscode/settings.json | 5 ++ LICENSE | 33 ++++++++ README.md | 21 +++++ install.sh | 100 ++++++++++++++++++++++++ template.conf | 3 + template.logrotate | 8 ++ template.sh | 175 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 345 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 LICENSE create mode 100644 README.md create mode 100755 install.sh create mode 100644 template.conf create mode 100644 template.logrotate create mode 100755 template.sh diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6133120 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "editor.fontSize": 13, + "terminal.integrated.fontSize": 13, + "window.zoomLevel": 1.4, + } \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5f42a2b --- /dev/null +++ b/LICENSE @@ -0,0 +1,33 @@ +Non-Commercial Use License – [template.sh, install.sh] + +Copyright (c) [2025] [Gilles Mouchet] + +These scripts are provided free of charge with their source code. Anyone is authorized to: + +- Use the scripts for personal, educational, or professional non-commercial purposes. +- Study, modify, and share the scripts free of charge, provided they retain this license. + +It is strictly prohibited to: + +- Sell these scripts or a modified version. +- Use them in commercial services or products. +- Distribute them in exchange for financial compensation, whether direct or indirect. + +These scripts are provided "as is," without warranty of any kind. +----------------------------------------------------------------------------------------- + Licence d’utilisation non commerciale – [template.sh, install.sh] + +Copyright (c) [2025] [Gilles Mouchet] + +Ces scripts sont fournis gratuitement avec leurs codes source. Toute personne est autorisée à : + +- Utiliser les scripts à des fins personnelles, éducatives ou professionnelles non commerciales. +- Étudier, modifier et partager les scripts gratuitement, à condition de conserver cette licence. + +Il est strictement interdit de : + +- Vendre ces scripts ou une version modifiée. +- L’utiliser dans des services ou produits commerciaux. +- Le distribuer en échange d’une contrepartie financière, directe ou indirecte. + +Ces scripts sont fourni "tel quel", sans garantie d’aucune sorte. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..56efa2c --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# Project Name +TODO: Write a project description +## Requirements +TODO: Requirments +## Installation +TODO: Describe the installation process +## Usage +TODO: Write usage instructions + +### Changelog +### [1.0.0] - 2025-09-06 +#### Added +- New features and functionality. +#### Modified +- Changes to existing functionality (backwards compatible). +#### Fixed +- Bug fixes. +#### Removed +- Deprecated or removed features (breaking changes). +#### Project initialization +- initialization by [GMo](mailto:gilles.mouchet@gmail.com) diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..271de7b --- /dev/null +++ b/install.sh @@ -0,0 +1,100 @@ +#!/bin/bash +############################################################ +# Decription: Install script +# Author: Gilles Mouchet (gilles.mouchet@gmail.com) +# Creation Date: 06-Sep-2025 +# Version: 1.0 +# +# Changelog: +# V1.0.0 - 06-Sep-2025 - GMo +# Added +# - Creation of script from scratch +# +############################################################ + +#----------------------------------------------------------- +# FUNCTIONS +#----------------------------------------------------------- +# Function installPacakege if needed +installPackage() { + packageName="$1" + echo "Installation of $packageName..." + case "$packageManager" in + apt) + sudo apt update && sudo apt install -y "$packageName" + ;; + dnf) + sudo dnf install -y "$packageName" + ;; + esac + if [ $? -eq 0 ]; then + echo "$packageName installed successfully" + else + echo "Error installing $packageName" + exit 1 + fi +} +#----------------------------------------------------------- +# variables + +fullScriptName=template.sh +shortScriptName=`echo $fullScriptName | sed -e 's|.*/||g' | cut -f1 -d.` +destPath=/usr/local/bin/ +configFile=$shortScriptName.conf +configFilePath=/etc/$shortScriptName/ +logRotateFile=$shortScriptName +logRotateFilePath=/etc/logrotate.d/$logRotateFile + +# 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 + +# select packet manager if need to install package during install +packageManager="" +if command -v apt &> /dev/null; then + packageManager="apt" +elif command -v dnf &> /dev/null; then + packageManager="dnf" +else + echo "Erreur : No supported package managers (apt, dnf) were found." + exit 1 +fi +echo "Package manager detected: $packageManager" + +# check if logPath exist +if [ ! -d $configFilePath ]; then + mkdir $configFilePath &> /dev/null + rc=$? + if [ "$rc" != "0" ];then + echo "[ERROR] - An error occurred while creating $configFilePath ($rc)" + else echo "[SUCCESS] - The folder $configFilePath was created successfully." + fi +fi +# install package (example) +installPackage mutt &> /dev/null + +# install script +cp $fullScriptName $destPath/. &> /dev/null +rc=$? +if [ "$rc" != "0" ];then + echo "[ERROR] - An error occurred while copying $fullScriptName to $destPath ($rc)" +else echo "[SUCCESS] - The script ${fullScriptName} to $destPath was copied successfully." +fi + +# copy config file +cp $configFile $configFilePath &> /dev/null +rc=$? +if [ "$rc" != "0" ];then + echo "[ERROR] - An error occurred while copying $configFile to $configFilePath ($rc)" +else echo "[SUCCESS] - The script $configFile to $configFilepath was copied successfully." +fi + +cp $logRotateFile.logrotate $logRotateFilePath &> /dev/null +rc=$? +if [ "$rc" != "0" ];then + echo "[ERROR] - An error occurred while copying $logRotateFile.logrotate to $logRotateFilePath ($rc)" +else echo "[SUCCESS] - The script $logRotateFile.logrotate to $logRotateFilePath was copied successfully." +fi +echo "Installation completed." \ No newline at end of file diff --git a/template.conf b/template.conf new file mode 100644 index 0000000..e048c30 --- /dev/null +++ b/template.conf @@ -0,0 +1,3 @@ +# mail recipient +msgRecipient=exploit.gmotech@gmail.com + diff --git a/template.logrotate b/template.logrotate new file mode 100644 index 0000000..e036149 --- /dev/null +++ b/template.logrotate @@ -0,0 +1,8 @@ +/var/log/template/template.log { + daily + rotate 7 + compress + missingok + notifempty + create 644 root root +} \ No newline at end of file diff --git a/template.sh b/template.sh new file mode 100755 index 0000000..f11784a --- /dev/null +++ b/template.sh @@ -0,0 +1,175 @@ +#!/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 \ No newline at end of file