From 5b3352345c5475750c4754f179b685b6c083d906 Mon Sep 17 00:00:00 2001 From: Alexandre CATTEAU Date: Fri, 19 Aug 2022 13:50:18 +0200 Subject: [PATCH] Initial commit --- .gitignore | 2 ++ README.md | 16 ++++++++++++++++ boot-sms.sh | 16 ++++++++++++++++ install.sh | 28 ++++++++++++++++++++++++++++ systemd-templates/boot-sms.service | 11 +++++++++++ variables.conf.template | 6 ++++++ wait-reaching.sh | 5 +++++ 7 files changed, 84 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 boot-sms.sh create mode 100755 install.sh create mode 100644 systemd-templates/boot-sms.service create mode 100644 variables.conf.template create mode 100755 wait-reaching.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..99a3140 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +variables.conf +.disabled diff --git a/README.md b/README.md new file mode 100644 index 0000000..a5fae9d --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# Boot SMS + +This script uses Free Mobile SMS API to send an SMS informing admin of server reboot. + +### Files +* `boot-sms.sh` is the main script. +* `wait-reaching.sh` is used by main script to make sure API service is reachable (in practice, delay is the time + required by network to be up). +* `variables.conf.template` contains examples variables definitions, and should be copied locally to `variables.conf` + (with any required modifications). +* `systemd-templates/` contains a Systemd units template for running the script at boot. +#### Installation +* `install.sh` script copies the Systemd template to its destination, and enable the service. + +### TODO +* RAS diff --git a/boot-sms.sh b/boot-sms.sh new file mode 100755 index 0000000..dbeeaaa --- /dev/null +++ b/boot-sms.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +source "$run_directory/variables.conf" + +# Set parameters here +host=$(hostname -f) +sysboot=$(who -b | sed 's/^[ \t]*//;s/[ \t]*$//') +time=$(date "+%Y-%m-%d %H:%M:%S") +msg="$host has rebooted. $sysboot If you did not initiate this, a power failure has probably occured. Message sent on $time." +msg=$(echo $msg | sed -e "s/ /%20/g") # Replace spaces with %20 as we are in an URL + +# Actual process +URL="https://smsapi.free-mobile.fr/sendmsg?user=$user&pass=$pass&msg=$msg" +curl $URL + +exit 0 diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..c4d1b11 --- /dev/null +++ b/install.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +run_directory=$(dirname "$0") +user=$(whoami) + +if [ -f $run_directory/.disabled ]; then + echo "Installation is disabled. Exiting..." + exit 0 +fi + +if [[ $user != 'root' ]]; then + sudo="sudo" +else + sudo="" +fi + +echo "Copying WebSSH unit to system directory..." +$sudo cp systemd-templates/boot-sms.service /etc/systemd/system/ +$sudo sed -i -e "s:PH_DIRECTORY:$(pwd):g" /etc/systemd/system/boot-sms.service + +echo "Reloading Systemd..." +$sudo systemctl daemon-reload + +echo "Enabling boot-sms.service..." +$sudo systemctl enable boot-sms.service + +echo "Finished install. Exiting..." +exit 0 diff --git a/systemd-templates/boot-sms.service b/systemd-templates/boot-sms.service new file mode 100644 index 0000000..78ba29a --- /dev/null +++ b/systemd-templates/boot-sms.service @@ -0,0 +1,11 @@ +[Unit] +Description=Send an SMS at system boot +After=network.target + +[Service] +Type=oneshot +ExecStartPre=/usr/bin/bash PH_DIRECTORY/wait-reaching.sh smsapi.free-mobile.fr +ExecStart=PH_DIRECTORY/boot-sms.sh + +[Install] +WantedBy=multi-user.target diff --git a/variables.conf.template b/variables.conf.template new file mode 100644 index 0000000..652d188 --- /dev/null +++ b/variables.conf.template @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +# Copy this file as variables.conf, with the correct values + +user='' +pass='' diff --git a/wait-reaching.sh b/wait-reaching.sh new file mode 100755 index 0000000..849ce90 --- /dev/null +++ b/wait-reaching.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +until ping -c 1 $1 > /dev/null 2>&1; do + sleep 1; +done