2023-02-01 23:22:02 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
# DESCRIPTION
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# ==============================================================================
|
2023-01-26 23:01:56 +01:00
|
|
|
|
|
|
|
#verify run as root or sudo
|
|
|
|
if [[ $EUID -ne 0 ]]; then
|
|
|
|
echo "This script must be run as root"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-01-27 15:36:13 +01:00
|
|
|
readonly OFFSET_OPTIONS=3
|
|
|
|
declare -a TARGETS
|
2023-02-01 23:22:02 +01:00
|
|
|
NUM_HOSTS=0
|
2023-01-26 23:01:56 +01:00
|
|
|
|
2023-01-27 15:36:13 +01:00
|
|
|
show_ssh_hosts () {
|
|
|
|
SSH_HOSTS_CONFIG_FILE="/root/.ssh/config"
|
|
|
|
|
|
|
|
if [ -f $SSH_HOSTS_CONFIG_FILE ]; then
|
|
|
|
TARGETS=($(cat $SSH_HOSTS_CONFIG_FILE | grep -E "^Host [^\*\*]" | sed "s/Host //"))
|
|
|
|
# get length of an array
|
2023-02-01 23:22:02 +01:00
|
|
|
NUM_HOSTS=${#TARGETS[@]}
|
|
|
|
for (( j=0; j<NUM_HOSTS; j++ ));
|
2023-01-27 15:36:13 +01:00
|
|
|
do
|
|
|
|
#printf "Current index %d with value %s\n" $j "${TARGETS[$j]}"
|
|
|
|
echo "$((j+$OFFSET_OPTIONS))) ${TARGETS[$j]}"
|
|
|
|
done
|
|
|
|
else
|
|
|
|
echo "NOTE: Config hosts file not found"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:22:02 +01:00
|
|
|
#is_root () {
|
|
|
|
#echo "test root"
|
|
|
|
#echo "$(id -u)"
|
|
|
|
# return $(id -u)
|
|
|
|
#}
|
|
|
|
|
|
|
|
has_sudo() {
|
|
|
|
local prompt
|
|
|
|
|
|
|
|
prompt=$(sudo -nv 2>&1)
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
echo "has_sudo__pass_set"
|
|
|
|
elif echo $prompt | grep -q '^sudo:'; then
|
|
|
|
echo "has_sudo__needs_pass"
|
|
|
|
else
|
|
|
|
echo "no_sudo"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
elevate_cmd () {
|
|
|
|
local cmd=$@
|
|
|
|
|
|
|
|
HAS_SUDO=$(has_sudo)
|
|
|
|
|
|
|
|
case "$HAS_SUDO" in
|
|
|
|
has_sudo__pass_set)
|
|
|
|
sudo $cmd
|
|
|
|
;;
|
|
|
|
has_sudo__needs_pass)
|
|
|
|
echo "Please supply sudo password for the following command: sudo $cmd"
|
|
|
|
sudo $cmd
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Please supply root password for the following command: su -c \"$cmd\""
|
|
|
|
su -c "$cmd"
|
|
|
|
;;
|
|
|
|
esac
|
2023-01-28 22:33:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
remote_update () {
|
|
|
|
ssh $1 << EOF
|
2023-02-01 23:22:02 +01:00
|
|
|
$(typeset -f has_sudo)
|
|
|
|
$(typeset -f elevate_cmd)
|
|
|
|
elevate_cmd apt update -qq
|
|
|
|
elevate_cmd apt upgrade -y
|
|
|
|
elevate_cmd apt autoremove
|
2023-01-28 22:33:49 +01:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2023-01-27 15:36:13 +01:00
|
|
|
#for i in "${targets[@]}"
|
|
|
|
#do
|
|
|
|
# :
|
|
|
|
# do whatever on "$i" here
|
|
|
|
# echo $i
|
|
|
|
|
|
|
|
|
|
|
|
echo "###################################"
|
|
|
|
echo "Auto Remote Updater"
|
|
|
|
echo -e "###################################\n"
|
|
|
|
|
|
|
|
finished=false
|
|
|
|
while [ "$finished" != "true" ]
|
|
|
|
do
|
|
|
|
|
|
|
|
# Choose script to install
|
|
|
|
echo "0) Sortir de l'script"
|
|
|
|
echo "1) current host: $(hostname)"
|
|
|
|
echo "2) custom host"
|
|
|
|
show_ssh_hosts
|
|
|
|
|
|
|
|
choosed=false
|
|
|
|
while [ "$choosed" != "true" ]
|
|
|
|
do
|
|
|
|
read -p 'Indica quin host vols actualitzar (0 per sortir): ' HOST
|
|
|
|
case $HOST in
|
|
|
|
("") echo "El número no pot ser buit";;
|
|
|
|
(*[!0-9]*) echo "Has d'introduir un número";;
|
|
|
|
(*) choosed=true #echo "just numeric";;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ "$HOST" = "0" ]; then
|
|
|
|
finished=true
|
|
|
|
exit 0
|
|
|
|
elif [ "$HOST" = "1" ]; then
|
2023-02-01 23:22:02 +01:00
|
|
|
apt update -qq
|
|
|
|
apt upgrade -y
|
|
|
|
apt autoremove
|
2023-01-27 15:36:13 +01:00
|
|
|
elif [ "$HOST" = "2" ]; then
|
|
|
|
read -p 'Indica el host remot: ' CUSTOM
|
|
|
|
echo $CUSTOM
|
2023-02-01 23:22:02 +01:00
|
|
|
elif [ "$((HOST-OFFSET_OPTIONS))" -lt $NUM_HOSTS ]; then
|
|
|
|
index=$((HOST-OFFSET_OPTIONS))
|
2023-01-27 15:36:13 +01:00
|
|
|
echo "${TARGETS[$index]}"
|
2023-01-28 22:33:49 +01:00
|
|
|
remote_update "${TARGETS[$index]}"
|
2023-02-01 23:22:02 +01:00
|
|
|
else
|
|
|
|
echo -e "Aquest número de host no existeix. Tornar a intentar-ho\n"
|
2023-01-27 15:36:13 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
done
|