„runupdate“ ändern

This commit is contained in:
Yann Mölle 2023-04-19 19:51:55 +02:00
parent 4fdd531b9e
commit 59eb1301e6

View File

@ -1,61 +1,50 @@
#!/bin/bash #!/bin/bash
# Read the server list from the specified file # List of servers to update
SERVER_LIST_FILE="servers.conf" servers=(10.0.0.50 10.0.0.51)
SERVER_LIST=()
while IFS= read -r SERVER || [[ -n "$SERVER" ]]; do
SERVER_LIST+=("$SERVER")
done < "$SERVER_LIST_FILE"
# Loop through the server list # SSH username for remote access
for SERVER in "${SERVER_LIST[@]}" user=root
do
echo "Checking for updates on server: ${SERVER}"
# Establish SSH connection to remote server and run commands to check for updates # Function to check for updates on a remote server
SSH_RESULT=$(ssh root@${SERVER} "sudo apt-get update > /dev/null && apt list --upgradable") check_updates () {
ssh $user@$1 "sudo apt-get update > /dev/null && apt list --upgradable"
}
# If updates are available, inform the user # Function to update a remote server
if [ -n "${SSH_RESULT}" ]; then update_server () {
echo "Updates available on server ${SERVER}:" ssh $user@$1 "sudo apt-get update > /dev/null && sudo apt-get upgrade -y" > /dev/null 2>&1
echo "${SSH_RESULT}" if [ $? -eq 0 ]; then
echo "" echo "Update successful on $1"
else else
echo "No updates available on server ${SERVER}" echo "Update failed on $1"
echo "" fi
}
# Loop through servers and check for updates
for server in "${servers[@]}"
do
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 fi
done done
# Prompt user to confirm if they want to update the listed servers # Prompt user to confirm update
read -p "Do you want to update the listed servers? (Yes/No): " UPDATE_CONFIRMATION read -p "Do you want to update these servers? (y/n): " response
if [ "$response" != "y" ]; then
# If the user confirms with "Yes", update the servers and store the update status for each server in a list exit 0
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 fi
# Loop through servers and update
for server in "${servers[@]}"
do
echo "Updating $server"
update_server $server
done done
# Summary of update status echo "Updates completed"
echo ""
echo "Summary of update status:"
for STATUS in "${UPDATE_STATUS[@]}"
do
echo "${STATUS}"
done
else
echo "Updates cancelled."
fi