Initial commit

This commit is contained in:
Alexandre CATTEAU 2022-08-19 13:50:18 +02:00
commit 5b3352345c
7 changed files with 84 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
variables.conf
.disabled

16
README.md Normal file
View File

@ -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

16
boot-sms.sh Executable file
View File

@ -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

28
install.sh Executable file
View File

@ -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

View File

@ -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

6
variables.conf.template Normal file
View File

@ -0,0 +1,6 @@
#!/usr/bin/env bash
# Copy this file as variables.conf, with the correct values
user=''
pass=''

5
wait-reaching.sh Executable file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env bash
until ping -c 1 $1 > /dev/null 2>&1; do
sleep 1;
done