From 59eb1301e6e43d519771d21d019fc17ac53c30ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20M=C3=B6lle?= Date: Wed, 19 Apr 2023 19:51:55 +0200 Subject: [PATCH] =?UTF-8?q?=E2=80=9Erunupdate=E2=80=9C=20=C3=A4ndern?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- runupdate | 97 ++++++++++++++++++++++++------------------------------- 1 file changed, 43 insertions(+), 54 deletions(-) diff --git a/runupdate b/runupdate index 28bda80..4bb8a9a 100644 --- a/runupdate +++ b/runupdate @@ -1,61 +1,50 @@ #!/bin/bash -# Read the server list from the specified file -SERVER_LIST_FILE="servers.conf" -SERVER_LIST=() -while IFS= read -r SERVER || [[ -n "$SERVER" ]]; do - SERVER_LIST+=("$SERVER") -done < "$SERVER_LIST_FILE" +# List of servers to update +servers=(10.0.0.50 10.0.0.51) -# Loop through the server list -for SERVER in "${SERVER_LIST[@]}" +# SSH username for remote access +user=root + +# Function to check for updates on a remote server +check_updates () { + ssh $user@$1 "sudo apt-get update > /dev/null && apt list --upgradable" +} + +# Function to update a remote server +update_server () { + ssh $user@$1 "sudo apt-get update > /dev/null && sudo apt-get upgrade -y" > /dev/null 2>&1 + if [ $? -eq 0 ]; then + echo "Update successful on $1" + else + echo "Update failed on $1" + fi +} + +# Loop through servers and check for updates +for server in "${servers[@]}" do - echo "Checking for updates on server: ${SERVER}" - - # Establish SSH connection to remote server and run commands to check for updates - SSH_RESULT=$(ssh root@${SERVER} "sudo apt-get update > /dev/null && apt list --upgradable") - - # If updates are available, inform the user - if [ -n "${SSH_RESULT}" ]; then - echo "Updates available on server ${SERVER}:" - echo "${SSH_RESULT}" - echo "" - else - echo "No updates available on server ${SERVER}" - echo "" - fi + echo "Checking for updates on $server" + updates=$(check_updates $server) + if [ -n "$updates" ]; then + echo "Updates available on $server:" + echo "$updates" + else + echo "No updates available on $server" + fi done -# Prompt user to confirm if they want to update the listed servers -read -p "Do you want to update the listed servers? (Yes/No): " UPDATE_CONFIRMATION - -# If the user confirms with "Yes", update the servers and store the update status for each server in a list -if [[ $UPDATE_CONFIRMATION == "Yes" ]]; then - UPDATE_STATUS=() - for SERVER in "${SERVER_LIST[@]}" - do - echo "Updating server: ${SERVER}" - - # Establish SSH connection to remote server and run commands to update - ssh root@${SERVER} "sudo apt-get update > /dev/null && sudo apt-get upgrade -y" > /dev/null 2>&1 - - # Check the return value of ssh to determine if the update was successful - if [[ $? -eq 0 ]]; then - UPDATE_STATUS+=("${SERVER}: Update successful") - echo "Server ${SERVER} updated." - else - UPDATE_STATUS+=("${SERVER}: Update failed") - echo "Failed to update server ${SERVER}." - fi - done - - # Summary of update status - echo "" - echo "Summary of update status:" - for STATUS in "${UPDATE_STATUS[@]}" - do - echo "${STATUS}" - done -else - echo "Updates cancelled." +# Prompt user to confirm update +read -p "Do you want to update these servers? (y/n): " response +if [ "$response" != "y" ]; then + exit 0 fi + +# Loop through servers and update +for server in "${servers[@]}" +do + echo "Updating $server" + update_server $server +done + +echo "Updates completed"