FUN WITH LINUX

Half-Automatic Update-Script(apt)

12 November 2014

I want to have control over debian updates, but I don’t want to make them manually. So I decided to do it “half-automatic”. This means that they run automatically until user-input is needed. The whole prozess is recorded in logfiles and after the updates are done this script sends out emails.

This is the parser for the summary-email. it works for english ang german environments:

#!/usr/bin/perl

use strict;

if ( $#ARGV ne 0)
{
        die "usage: $0 \n";
}

my $logfile = $ARGV[0];
my $upgrades = undef;

open(LOG,"< $logfile") or die "can't open logfile: $logfile";
while()
{
        my $line = $_;

        if($line =~ /The following packages will be upgraded/g)
        {
                $line = ;
                while($line !~ /\d+ upgraded, \d+ newly installed, \d+ to remove and \d+ not upgraded/)
                {
                        $upgrades = $upgrades . " " . $line;
                        $line = ;
                }
        }

        if($line =~ /Die folgenden Pakete werden aktualisiert/g)
        {
                $line = ;
                while($line !~ /\d+ aktualisiert, \d+ neu installiert, \d+ zu entfernen und \d+ nicht aktualisiert./)
                {
                        $upgrades = $upgrades . " " . $line;
                        $line = ;
                }
        }


}
close(LOG);

$upgrades =~ s/^\s+//g;
$upgrades =~ s/\r+//g;
$upgrades =~ s/\n+//g;

if($upgrades =~ /^\s+$/g or $upgrades =~ /^$/g or $upgrades =~ /^\n$/g)
{
        exit 0;
}

my @arr = split(/\s+/,$upgrades);
print "$upgrades \n";
exit $#arr + 1;

And this is our update-script:

#!/bin/bash

LOGDIR=/opt/update-logs
PARSER=/opt/bin/aptlogparser.pl

eval `ssh-agent -s`
ssh-add

export TERM="rxvt"

function update_customer
{
        for host in $HOSTS
        do
                DAT=`date +%F-%R`
                echo "KUNDE: $KUNDE"
                script -c "ssh -l root $host \"export TERM=rxvt; export DEBIAN_FRONTEND=readline; echo $TERM; hostname; apt-get update; apt-get upgrade\""
                test -e typescript && mv typescript $LOGDIR/$host-$DAT.log
                UPDATES=`$PARSER $LOGDIR/$host-$DAT.log`
                if [$? -ne 0]
                then
                        echo "$host: $UPDATES" >> ${KUNDE}_EMAIL.txt
                        echo "" >> ${KUNDE}_EMAIL.txt
                fi
        done

        DAT=`date +%F`
        if [-e ${KUNDE}_EMAIL.txt]
        then
                cat ${KUNDE}_EMAIL.txt | mutt -s "Linux-Updates vom $DAT" -- $EMAIL
                rm ${KUNDE}_EMAIL.txt
        fi
}


HOSTS="websrv mailsrv"
EMAIL="bob@example.com"
KUNDE="customer1"
update_customer

HOSTS="linuxsrv1 linuxsrv2"
EMAIL="alice@example.com"
KUNDE="customer2"
update_customer
[ Linux  Programming  Sysadmin  Bash  Perl  ]
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 Unported License.

Copyright 2015-present Hoti