Created
November 28, 2020 05:15
-
-
Save Radcliffe/08a133f96e2e99abe8d021b719efabe3 to your computer and use it in GitHub Desktop.
Check for security updates on an Ubuntu server and send email notifications via Sendgrid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script to send an email notification via Sendgrid when there are security updates pending for an Ubuntu server. | |
# Sendgrid API reference | |
# https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html | |
# Bash Guide on if-statements | |
# https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html | |
# Ask Ubuntu: How to get a list of security updates | |
# https://askubuntu.com/questions/774805/how-to-get-a-list-of-all-pending-security-updates | |
##### Change the following lines | |
sendgrid_api_key="Your Sendgrid API key" | |
hostname=`hostname` | |
from="[email protected]" | |
to="[email protected]" | |
subject="Security updates available" | |
##### | |
updates=`apt-get upgrade -s | grep "Inst.*-security" | wc -l` | |
if [ $updates -ne "0" ] | |
then | |
mailmsg=`cat <<EOM | |
{ | |
"personalizations": [ | |
{ | |
"to": [ | |
{ | |
"email": "$to" | |
} | |
], | |
"subject": "$subject" | |
} | |
], | |
"from": { | |
"email": "$from" | |
}, | |
"content": [ | |
{ | |
"type": "text/plain", | |
"value": "There are $updates pending security updates on $hostname." | |
} | |
] | |
} | |
EOM` | |
curl -X "POST" "https://api.sendgrid.com/v3/mail/send" -H "Authorization: Bearer $sendgrid_api_key" \ | |
-H "Content-Type: application/json" -d "$mailmsg" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment