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"
cpasqualini
 

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

cpasqualini
 

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 keep or sell it.
aker

WSUS Offline Update „Community Edition“
https://gitlab.com/wsusoffline/wsusoffline/-/releases
aker
 
Posts: 3999
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: 525
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
 

Re: wsus self update bash script

Postby Fred928 » 10.03.2020, 22:53

Hello, just a little note, since version 11.9, the page is in HTTPS, so the script must be corrected to initiate the variable "FILE".


FILE=$(echo $URL |sed 's/http:\/\/download.wsusoffline.net\///') ==> Wrong formula
FILE=$(echo $URL |sed 's/https:\/\/download.wsusoffline.net\///') ==> Correct formula

I allow myself to propose the complete script with this correction

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/https:\/\/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

Fred928
 
Posts: 4
Joined: 04.04.2019, 20:30

Re: wsus self update bash script

Postby Rush » 12.07.2020, 22:49

can you make an update for the new community version?
viewtopic.php?f=7&t=10194
Rush
 
Posts: 177
Joined: 09.08.2015, 19:05

Re: wsus self update bash script

Postby Rush » 14.05.2021, 12:05

:)?
Rush
 
Posts: 177
Joined: 09.08.2015, 19:05


Return to Linux

Who is online

Users browsing this forum: No registered users and 27 guests