wsus self update bash script

wsus self update bash script

Postby cpasqualini » 17.03.2015, 15:19

Hi!

I don't know were to post this to the maintainers, i hope they read this.
I am not a very good script writer, bit this works as expected since about a year with good results:

Code: Select all
#!/bin/bash

#
# Author:       Carlos Pasqualini
# email:         carlos [at] carlospasqualini.com.ar
# Website:     http://www.carlospasqualini.com.ar/
#
# License:      GPLv3+
#               ../doc/license.txt
#

# disable Debugging
#DEBUG=true
# enable Debugging
DEBUG=echo

# Go to script's path as a start:
cd `dirname $0`

dos2unix -q -n ../doc/history.txt ../temp/history.txt
OLDURL=`head ../temp/history.txt |grep 'WSUS Offline Update '|sed 's/\.//g;s/^WSUS Offline Update /http:\/\/download.wsusoffline.net\/wsusoffline/'|awk '{print $0 ".zip"}'`

URL=`wget -q http://download.wsusoffline.net/rss.xml -O - | grep zip |sed 's/^[ \t]*<link>//;s/<\/link>//'`

$DEBUG OLDURL: $OLDURL
$DEBUG NEWURL: $URL

if [[ $OLDURL != $URL ]]; then
        $DEBUG We need to update wsusoffline!
        cd ../temp/
        wget -q $URL
        HASH=`echo $URL |sed 's/\.zip/_hashes.txt/'`
        wget -q $HASH
        FILE=`echo $URL |sed 's/http:\/\/download.wsusoffline.net\///'`
        HASH=`echo $FILE|sed 's/\.zip/_hashes.txt/'`
        if [[ -f $FILE ]]; then
                SHA256=`sha256sum ../temp/$FILE | awk '{print $1}'`
                if [[ `grep -c "$SHA256,$FILE" $HASH` -gt 0 ]]; then
                        echo Download validated
                        cd ../temp/
                        if [[ -d wsusoffline ]]; then
                                rm -r wsusoffline
                        fi
                        unzip -q $FILE
                        cd ..
                        cp -av temp/wsusoffline/* .
                else
                        echo Download failed
                        if [[ -f ../temp/$FILE ]]; then
                                rm -v ../temp/$FILE
                        fi
                        if [[ -f ../temp/$HASH ]]; then
                                rm -v ../temp/$HASH
                        fi
                        if [[ -d ../temp/wsusoffline ]]; then
                                rm -r ../temp/wsusoffline/
                        fi
                fi
        fi
fi


i hope this helps someone and obtain feedback to improve it

Thanks!
Last edited by aker on 12.02.2016, 22:49, edited 1 time in total.
Reason: marked as "important"
Carlos Pasqualini
http://www.carlospasqualini.com.ar/
cpasqualini
 
Posts: 3
Joined: 10.09.2014, 21:39

Re: wsus self update bash script

Postby cpasqualini » 12.02.2016, 20:13

This script continued to working as expected.
today i have received an email with suggestions that improve code quality, so he is the new version.

Thanks to S. Pfeffer for his suggestions

Code: Select all
#!/bin/bash

#
# Author:   Carlos Pasqualini
# email:   carlos [at] carlospasqualini.com.ar
# Website:   http://www.carlospasqualini.com.ar/
#
# Rev: 2
#
# Contributors:
#   S. Pfeffer
#
# License:   GPLv3+
#      ../doc/license.txt
#

# disable Debugging
DEBUG=true
# enable Debugging
#DEBUG=echo

cd $(dirname $0)

dos2unix -q -n ../doc/history.txt ../temp/history.txt
OLDURL=$(head ../temp/history.txt |grep 'WSUS Offline Update '|sed 's/\.//g;s/^WSUS Offline Update /http:\/\/download.wsusoffline.net\/wsusoffline/'|awk '{print $0 ".zip"}')

URL=$(wget -q http://download.wsusoffline.net/rss.xml -O - | grep zip |sed 's/^[ \t]*<link>//;s/<\/link>//')

$DEBUG OLDURL: $OLDURL
$DEBUG NEWURL: $URL

if [[ $OLDURL != $URL ]]; then
   echo We need to update wsusoffline!
   if [[ ! -d ../temp/ ]]; then
      mkdir ../temp/
   fi
   cd ../temp/
   wget -q $URL
   HASH=$(echo $URL |sed 's/\.zip/_hashes.txt/')
   wget -q $HASH
   FILE=$(echo $URL |sed 's/http:\/\/download.wsusoffline.net\///')
   HASH=$(echo $FILE|sed 's/\.zip/_hashes.txt/')
   if [[ -f $FILE ]]; then
      SHA256=$(sha256sum ../temp/$FILE | awk '{print $1}')
      if [[ $(grep -c "$SHA256,$FILE" $HASH) -gt 0 ]]; then
         echo Download validated
         cd ../temp/
         if [[ -d wsusoffline ]]; then
            rm -r wsusoffline
         fi
         unzip -q $FILE
         cd ..
         cp -av temp/wsusoffline/* .
         rm -r temp/wsusoffline/
      else
         echo Download failed
         if [[ -f ../temp/$FILE ]]; then
            rm -v ../temp/$FILE
         fi
         if [[ -f ../temp/$HASH ]]; then
            rm -v ../temp/$HASH
         fi
         if [[ -d ../temp/wsusoffline ]]; then
            rm -r ../temp/wsusoffline/
         fi
      fi
   fi
fi

Carlos Pasqualini
http://www.carlospasqualini.com.ar/
cpasqualini
 
Posts: 3
Joined: 10.09.2014, 21:39

Re: wsus self update bash script

Postby aker » 12.02.2016, 22:50

Your script semms to be helpful for Linux-users.
I marked this topic as "important", so it will be listed at the beginning of the "Linux"-forum.
Wer Rechtschreibfehler findet, darf sie behalten oder an den Meistbietenden versteigern. / Everybody finding a misspelling is allowed to sell it.
aker
aker
 
Posts: 2730
Joined: 02.03.2011, 15:32

Re: wsus self update bash script

Postby hbuhrmester » 13.02.2016, 06:47

Very well,

I just find the version check too complicated. The canonical way, as in the Windows script CheckOUVersion.cmd, would be:

  1. The old version is in the file ../static/StaticDownloadLink-this.txt
  2. The new version is in the file http://download.wsusoffline.net/StaticD ... recent.txt , which gets downloaded to the static directory.
  3. If these files differ, then a new version is available.

A minimal version check would be:

Code: Select all
#!/bin/bash
# Basic version check

wget --timestamping --directory-prefix="../static" \
    "http://download.wsusoffline.net/StaticDownloadLink-recent.txt"

diff --strip-trailing-cr \
    "../static/StaticDownloadLink-this.txt" \
    "../static/StaticDownloadLink-recent.txt" > /dev/null

if (( $? == 0 )); then
    echo "The installed version of WSUS Offline Update is up-to-date."
else
    echo "A new version of WSUS Offline Update is available."
fi
hbuhrmester
 
Posts: 219
Joined: 11.10.2013, 20:59

Re: wsus self update bash script

Postby spfef » 21.02.2016, 09:31

I've tested it with the simplified version check from hbuhrmester and fixed the cleanup and set permission section. You need to set the permission of the scripts after an update, because after an update they are no longer executeable

Der neue, einfachere Versionscheck von hbuhrmester ist nun eingebaut und getestet. Ausserdem funktioniert nun auch die Cleanup Funktion am Ende des Scipts. Diese hat zuvor nach einem erfolgreichen Update nicht funktioniert, aufgrund eines Verzeichniswechsels zuvor. Nach einem Update müssen die Scripte wieder ausführbar gemacht werden.

Code: Select all
#!/bin/bash
 
#
# Author:       Carlos Pasqualini
# email:        carlos [at] carlospasqualini.com.ar
# Website:      http://www.carlospasqualini.com.ar/

#
# Rev: 3
#
# Contributors:
#   spfef
#   hbuhrmester
#
# License:      GPLv3+
#               ../doc/license.txt
#
# disable Debugging
#DEBUG=true
# enable Debugging
DEBUG=echo
#
# Go to script's path as a start:
cd "$(dirname "$0")"
BASEPATH=$(pwd)
if [ ! -d ../temp ]; then
   mkdir ../temp
fi
wget --timestamping --directory-prefix="../static" \
    "http://download.wsusoffline.net/StaticDownloadLink-recent.txt"

diff --strip-trailing-cr \
    "../static/StaticDownloadLink-this.txt" \
    "../static/StaticDownloadLink-recent.txt" > /dev/null

if (( $? == 0 )); then
    echo "The installed version of WSUS Offline Update is up-to-date."
    exit 0;
else
    URL=$(cat ../static/StaticDownloadLink-recent.txt)
    $DEBUG We need to update wsusoffline!
    cd ../temp/
    wget -q $URL
    HASH=$(echo $URL |sed 's/\.zip/_hashes.txt/')
    wget -q $HASH
    FILE=$(echo $URL |sed 's/http:\/\/download.wsusoffline.net\///')
    HASH=$(echo $FILE|sed 's/\.zip/_hashes.txt/')
    if [[ -f $FILE ]]; then
        SHA256=$(sha256sum ../temp/$FILE | awk '{print $1}')
            if [[ $(grep -c "$SHA256,$FILE" $HASH) -gt 0 ]]; then
                echo Download validated
                cd ../temp/
                if [[ -d wsusoffline ]]; then
                    rm -r wsusoffline
                fi
                unzip -q $FILE
                cd ..
                cp -av temp/wsusoffline/* .
                else
                        echo Download failed
                        if [[ -f ../temp/$FILE ]]; then
                                rm -v ../temp/$FILE
                        fi
                        if [[ -f ../temp/$HASH ]]; then
                                rm -v ../temp/$HASH
                        fi
                        if [[ -d ../temp/wsusoffline ]]; then
                                rm -r ../temp/wsusoffline/
                        fi
            fi
    fi
fi
cd $BASEPATH
# cleanup
if [ -d ../temp ]; then
   rm -rf ../temp
fi
# if you have to set file permissions uncomment some out
# find ../ -type d -print0 | xargs -0 chmod 755
# find ../ -type f -print0 | xargs -0 chmod 644
# chown -R nobody.nogroup ../
#
# make the shell scripts executabal again
find ../ -name '*.sh' -print0 | xargs -0 chmod +x

spfef
 
Posts: 8
Joined: 21.02.2016, 09:16


Return to Linux

Who is online

Users browsing this forum: No registered users and 2 guests