Created
September 4, 2019 11:28
-
-
Save icefo/726f836cf6b91cebd045856f825839ae to your computer and use it in GitHub Desktop.
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
#! /usr/bin/env python3 | |
# this script assume that sendmail is working on your system | |
import socket | |
from email.mime.text import MIMEText | |
import subprocess | |
import re | |
subprocess.run(['apt', 'update'], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) | |
dry_run_update = subprocess.run( | |
['apt-get', 'upgrade', '--dry-run'], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT | |
).stdout.decode('utf-8') | |
dry_run_result = re.search( | |
r"^(\d+) upgraded, (\d+) newly installed, (\d+) to remove and (\d+) not upgraded\.$", | |
dry_run_update, | |
flags=re.MULTILINE | |
) | |
to_upgrade = int(dry_run_result.group(1)) | |
to_install = int(dry_run_result.group(2)) | |
if to_upgrade == 0 and to_install == 0: | |
exit() # nothing to upgrade | |
to_be_upgraded = re.search( | |
r"^The following packages will be upgraded:(.*)not upgraded\.$", | |
dry_run_update, | |
flags=re.DOTALL | re.MULTILINE | |
).group() | |
msg = MIMEText("Following is a dry-run of the upgrade:\n" + to_be_upgraded) | |
msg["From"] = "root@" + socket.getfqdn() | |
msg["To"] = "[email protected]" | |
msg["Subject"] = "There are updates to do on " + socket.getfqdn() | |
subprocess.run(["/usr/sbin/sendmail", "-t", "-oi"], input=msg.as_bytes(unixfrom=True)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment