Skip to content

Instantly share code, notes, and snippets.

@SR-G
Forked from thiagokokada/ [Arch Linux] auto_upgrade
Created January 9, 2016 18:34
Show Gist options
  • Save SR-G/fe056388548540f9b6a4 to your computer and use it in GitHub Desktop.
Save SR-G/fe056388548540f9b6a4 to your computer and use it in GitHub Desktop.
Get e-mail notifications of updates in Arch Linux

notify_update

Get e-mail notifications of updates in Arch Linux

Small Python script to send update notifications via e-mail in Arch Linux distribution (and derivatives). Needs Python 3.5 (that you should already have if you use Arch Linux) and cower (and pacman, of course).

Installation

Firstly, install cower if you don't already have it installed. It is necessary for AUR updates (of course, you can modify notify_updates.py yourself to remove cower support). Secondly, Open notify_updates.py and modify according your necessities (at least you need to modify EMAILand PASSWORD variables). Afterwards, copy notify_updates.py to /usr/local/bin and notify_updates.service and notify_updates.timer to /etc/systemd/system. Finally enable the timer service by running:

# systemctl enable notify_updates.timer

That's it.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2016 Thiago Kenji Okada <thiago.mast3r@gmail.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
#!/usr/bin/python3
import os
import smtplib
import sys
import time
from email.mime.text import MIMEText
from socket import gethostname
from subprocess import run, PIPE
# Needs to be a TLS protected SMTP server
SMTP_SERVER = "smtp.gmail.com"
SMTP_PORT = 587
EMAIL = "INSERT EMAIL HERE"
PASSWORD = "INSERT PASSWORD HERE"
SUBJECT_TEMPLATE = "Updates available in {hostname}"
MSG_TEMPLATE = """
At {time}, there are updates available in {hostname}.
{updates}
Login to {hostname} and run:
# pacman -Su
To update the system.
"""
def add_prefix(prefix, text):
result = ""
for line in text.splitlines():
result += prefix + line + "\n"
return result
def check_updates():
run(["pacman", "--sync", "--downloadonly", "--sysupgrade", "--refresh", "--quiet"], stdout=PIPE)
result = run(["pacman", "--query", "--upgrades"], stdout=PIPE, universal_newlines=True)
packages = ""
if result.stdout:
packages += "\nOfficial repositories:\n"
packages += add_prefix("\t* ", result.stdout)
# Comment the lines below if you don't want cower/AUR support
result = run(["cower", "--update", "--color=never"], stdout=PIPE, universal_newlines=True)
if result.stdout:
packages += "\nAUR:\n"
packages += add_prefix("\t* ", result.stdout)
return packages
def send_email(email, password, subject, message):
# Connect to SMTP server
server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
server.ehlo()
server.starttls()
server.login(email, password)
# Sender/Receiver should be the same e-mail
msg = MIMEText(message)
msg['Subject'] = subject
msg['From'] = email
msg['To'] = email
server.send_message(msg)
server.quit()
def main():
if os.geteuid() != 0:
sys.exit("You need root privileges to run {}".format(sys.argv[0]))
available_updates = check_updates()
if available_updates:
print("Available updates, sending e-mail to {}".format(EMAIL))
hostname = gethostname()
send_email(EMAIL, PASSWORD,
SUBJECT_TEMPLATE.format(hostname=hostname),
MSG_TEMPLATE.format(time=time.strftime("%c"),
hostname=hostname,
updates=available_updates)
)
else:
print("No available updates, not sending e-mail")
sys.exit()
if __name__ == "__main__":
main()
[Unit]
Description=Run notify_updates.py script
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/bin/python3 /usr/local/bin/notify_updates.py
[Install]
WantedBy=multi-user.target
[Unit]
Description=Run check_updates.py daily
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
@EugeneFlash
Copy link

Hello! This script is exactly what I need, thanks!!! But I have some ignored packeges in Arch and when there is no updates I recieve notifications too because of [ignored]. Can you tell me how to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment