Remote updates on debian

slackware | Δευ, 08/21/2017 - 19:08 | 3' | 3

Καλησπέρα,

Παρακάτω ακολουθεί ένα script το οποίο  ελέγχει ένα debian like OS για updates και αν βρει τα εγκαθηστά.

Απαραίτητη προυπόθεση ο root θα πρέπει να έχει ssh certificate για να κάνει login χωρίς να χρειαστεί να δώσει τον κωδικό πρόσβασης.

Επίσης οι remote servers δηλώνονται στο /tmp/remote.txt file.


#!/bin/sh

SSH=$(which ssh)
APT=$(which apt)
APTGET=$(which apt-get)
USERNAME='root'
FILE='/tmp/remote.txt'

ID=`id -ru`

if [ $ID -ne 0 ]
then
	echo "You must be root or use sudo in order to execute this script"
	exit
fi

#LOCAL_ETH=`ip ad sh dev eth0|grep -w inet|awk '{print $2}'|cut -d\/ -f1`
#LOCAL_WRL=`ip ad sh dev wlan0|grep -w inet|awk '{print $2}'|cut -d\/ -f1`

if [ ! -e $FILE ]
then
	echo "$FILE does not exists"
	exit
fi

REMOTE=`cat $FILE`

if [ x$REMOTE = "x" ]
then
	echo "No remote servers assigned"
	exit
else
	echo "Remote servers found..."
	for servers in $REMOTE
	do      
        echo -n "Checking if ssh is working on $servers..."
		$SSH [email protected]$servers exit || results="failed" 
		if [ x$results = "xfailed" ]
		then
			echo  "Failed"
			exit
		else
			echo  "OK"
		fi
	done
fi

for server in $REMOTE
do
	echo "Checking server $server for updates"
	$SSH [email protected]$server $APTGET update 2&> /dev/null
	count=`$SSH [email protected]$server $APT list --upgradable 2&>/dev/null|grep -vi listing|wc -l`
	if [ $count -eq 0 ]
	then
		echo "System is up-to-date"
	else
		echo "Updates found"
		echo "==============================================================================="
		pkgs=`$SSH [email protected]$server $APT list --upgradable 2&>/dev/null| grep -vi listing|cut -d\  -f1`
			
		for i in $pkgs
		do
			echo $i
		done
		echo "==============================================================================="
		echo "Total packages for update: $count"
		echo ""
        echo "In one minute the updates will be applied"
        sleep 60
		echo "Start system upgrade on server $server";  $SSH [email protected]$server $APTGET -qqq dist-upgrade -y 
	fi
done

 

Δώσε αστέρια!

MO: 3.8 (ψήφοι: 4)

Σχόλια

Πολύ χρήσιμο εργαλειο!!!! Αλλα πως ξέρεις ότι θα έχει τελειώσει το update σε 60 '' ;