Skip to main content

Raymii.org Raymii.org Logo

Quis custodiet ipsos custodes?
Home | About | All pages | Cluster Status | RSS Feed

Ubuntu/Debian update mailer

Published: 10-06-2012 | Author: Remy van Elst | Text only version of this article


❗ This post is over eleven years old. It may no longer be up to date. Opinions may have changed.

Table of Contents

  • The script

  • This is a bash script for Ubuntu and Debian, to be run by cron on a set interval. It checks if there are apt-updates, and if so, mails an overview of the packages which can be updated, whith their local version, the version available and an URL to the Ubuntu package site where you can see the changelog. I wrote this as an addition for my Nagios monitoring, that only shows how many updates there are, this is a lot more verbose. It also runs on Debian since 1 Nov 2012. Only the links don't work then, because the packages are different.

    Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:

    I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!

    Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.

    You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $200 credit for 60 days. Spend $25 after your credit expires and I'll get $25!

    Example output:

    If there are no updates:

    No updates available for host vps11.sparklingclouds.nl on date 21.09.2012
    

    If there are updates:

    --- Updates for host: vps11.sparklingclouds.nl ---
    Date: 21.09.2012 
    
    Total updates available: 44
    
    -- Package: apport --
     Installed: 2.0.1-0ubuntu12
     Candidate: 2.0.1-0ubuntu13
    Package Information: http://packages.ubuntu.com/precise/apport
    -- End package apport --
    
    -- Package: base-files --
     Installed: 6.5ubuntu6
     Candidate: 6.5ubuntu6.2
    Package Information: http://packages.ubuntu.com/precise/base-files
    -- End package base-files --
    
    -- Package: bind9-host --
     Installed: 1:9.8.1.dfsg.P1-4ubuntu0.2
     Candidate: 1:9.8.1.dfsg.P1-4ubuntu0.3
    Package Information: http://packages.ubuntu.com/precise/bind9-host
    -- End package bind9-host --
    
    -- Package: build-essential --
     Installed: 11.5ubuntu2
     Candidate: 11.5ubuntu2.1
    Package Information: http://packages.ubuntu.com/precise/build-essential
    -- End package build-essential --
    
    -- Package: dbus --
     Installed: 1.4.18-1ubuntu1
     Candidate: 1.4.18-1ubuntu1.1
    Package Information: http://packages.ubuntu.com/precise/dbus
    -- End package dbus --
    

    The script

    #!/bin/bash
    #Copyright (c) 2012 Remy van Elst
    #Permission is hereby granted, free of charge, to any person obtaining a copy
    #of this software and associated documentation files (the "Software"), to deal
    #in the Software without restriction, including without limitation the rights
    #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    #copies of the Software, and to permit persons to whom the Software is
    #furnished to do so, subject to the following conditions:
    #
    #The above copyright notice and this permission notice shall be included in
    #all copies or substantial portions of the Software.
    #
    #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    #THE SOFTWARE.
    # 01-11-2012 update version 2, now also supports Debian.
    
    
    if [ -f /etc/lsb-release ]; then
        TYPE="UBUNTU"
    elif [ -f /etc/debian_version ]; then
        TYPE="DEBIAN"
    fi
    
    case ${TYPE} in
        "UBUNTU")
            VERSION=`grep "DISTRIB_CODENAME" /etc/lsb-release | awk -F = '{ print $2 }'`   
            ;;
        "DEBIAN")
            VERSION="Debian `cat /etc/debian_version`"
            ;;    
    esac
    
    
    HOSTNAME=`hostname`
    EMAIL="user@domain.com"
    SUBJECT="Updates for host ${HOSTNAME}"
    DATE=`date +%d.%m.%Y`
    AVAIL_UPD=`apt-get -s upgrade | awk '/[0-9]+ upgraded,/ {print $1}'`
    
    function mailupdates {
    
    echo -e "--- Updates for host: ${HOSTNAME} ---"
    echo -e "Date: ${DATE} \n\n"
    
    echo -n "Total updates available: "
    apt-get -s upgrade | awk '/[0-9]+ upgraded,/ {print $1}'
    echo -e "\n\n"
    
    for i in `aptitude search ~U | cut -c 5- | awk '{ print $1  }'`; do 
        echo "-- Package: ${i} --"
        apt-cache policy ${i} | grep 'Installed\|Candidate'
        if [ $TYPE="UBUNTU" ]; then
            echo "Package Information: http://packages.ubuntu.com/${VERSION}/${i}"
        fi
        echo -e "-- End package ${i} --\n"
    done
    
    }
    
    
    if [[ ${AVAIL_UPD} == 0 ]]; then
        #echo "No updates available for host $HOSTNAME on date ${DATE}" | mail -s "${SUBJECT}" ${EMAIL}
        sleep 1
    else
        mailupdates | mail -s "${SUBJECT}" ${EMAIL}
    fi
    
    Tags: apt , bash , command-line , debian , software , ubuntu , update