Skip to content

Instantly share code, notes, and snippets.

@RobertHue
Last active March 12, 2021 12:45
Show Gist options
  • Save RobertHue/cbe8324a4b504168f2feee4b2dde4a1c to your computer and use it in GitHub Desktop.
Save RobertHue/cbe8324a4b504168f2feee4b2dde4a1c to your computer and use it in GitHub Desktop.
bash-script that updates the pi, pihole and sends a status via email
#!/bin/bash
# Description: This bash-script updates the pi, pihole and sends a status via email
# Author: RobHue
# Pre-Requisites:
# - install pihole (see https://docs.pi-hole.net/main/basic-install/)
# - "sudo apt install sendEmail libio-socket-ssl-perl libnet-ssleay-perl"
# - create a gmail account for your PI
# So this file can be executed: "sudo -i && chmod u+x pihole_task.sh"
# So no others can read the password: "chmod og-rwx pihole_task.sh"
# Usage: Add this script to the root's cron-jobs via "crontab -e"
# Example:
# update pi & pihole & send status via email (at 13:00 on Friday)
# 0 13 * * 5 /root/pihole_task.sh > /tmp/pihole_task.log 2>&1
echo $PATH
export PATH=$PATH:/etc/pihole:/usr/local/bin/ # for pihole
export PATH=$PATH:/usr/bin/
PIHOLE_TASK_FILE=/tmp/pihole_task.txt
echo 'Pihole Update Report:' > $PIHOLE_TASK_FILE
# https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
set -e # exit immediately if a non-zero status is returned
# update the pi (linux) itself (with apt package manager)
apt update && apt upgrade -y && apt autoremove -y
# update the pihole
pihole -up >> $PIHOLE_TASK_FILE
# update the pihole's gravity lists
pihole -g >> $PIHOLE_TASK_FILE
# send e-mail to myself
SMTP_FROM=ENTER_SENDER_EMAIL
SMTP_TO=ENTER_RECIPIENT_EMAIL
SUBJECT="[PI] system and pihole update"
SMTP_SERVER=smtp.googlemail.com:587
SMTP_USER="ENTER_SENDER_USER"
SMTP_PASS="ENTER_SENDER_PASSWORD"
echo 'Disk Usage:' >> $PIHOLE_TASK_FILE
df -h | grep -E 'Filesystem|Dateisystem' >> $PIHOLE_TASK_FILE
df -h | grep /dev/root >> $PIHOLE_TASK_FILE
df -h | grep /dev/mmcblk0p1 >> $PIHOLE_TASK_FILE
sendEmail -l /tmp/pihole_task.log \
-f $SMTP_FROM \
-t $SMTP_TO \
-u $SUBJECT \
-s $SMTP_SERVER \
-o tls=yes \
-xu $SMTP_USER \
-xp $SMTP_PASS \
-o message-file=$PIHOLE_TASK_FILE
cat /tmp/pihole_task.log >> $PIHOLE_TASK_FILE
echo 'Successfully updated pi & pihole' >> $PIHOLE_TASK_FILE
rm $PIHOLE_TASK_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment