2023-04-19 19:28:17 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2023-04-19 19:30:23 +02:00
|
|
|
# Read the server list from the specified file
|
2023-04-19 19:42:19 +02:00
|
|
|
SERVER_LIST_FILE="servers.conf"
|
2023-04-19 19:30:23 +02:00
|
|
|
SERVER_LIST=()
|
|
|
|
while IFS= read -r SERVER || [[ -n "$SERVER" ]]; do
|
|
|
|
SERVER_LIST+=("$SERVER")
|
|
|
|
done < "$SERVER_LIST_FILE"
|
2023-04-19 19:28:17 +02:00
|
|
|
|
|
|
|
# Loop through the server list
|
|
|
|
for SERVER in "${SERVER_LIST[@]}"
|
|
|
|
do
|
|
|
|
echo "Checking for updates on server: ${SERVER}"
|
|
|
|
|
|
|
|
# Establish SSH connection to remote server and run commands to check for updates
|
2023-04-19 19:39:10 +02:00
|
|
|
SSH_RESULT=$(ssh username@${SERVER} "sudo apt-get update > /dev/null && apt list --upgradable")
|
2023-04-19 19:28:17 +02:00
|
|
|
|
|
|
|
# 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
|
|
|
|
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
|
2023-04-19 19:39:10 +02:00
|
|
|
ssh username@${SERVER} "sudo apt-get update > /dev/null && sudo apt-get upgrade -y" > /dev/null 2>&1
|
2023-04-19 19:28:17 +02:00
|
|
|
|
|
|
|
# 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."
|
|
|
|
fi
|