batchupdate/runupdate

56 lines
1.3 KiB
Bash

#!/bin/bash
# Read the server list from a file
servers_file="servers.conf"
if [ ! -f "$servers_file" ]; then
echo "Error: $servers_file does not exist."
exit 1
fi
servers=($(cat "$servers_file"))
# SSH username for remote access
user=root
# Function to check for updates on a remote server
check_updates () {
ssh $user@$1 "apt-get update > /dev/null && apt list --upgradable"
}
# Function to update a remote server
update_server () {
ssh $user@$1 "apt-get update > /dev/null && apt-get upgrade -y" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Update successful on $1"
elif [ -n "$updates" ]; then
echo "Update failed on $1"
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
done
# 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"