done remote_updater

This commit is contained in:
Roger Pàmies Fabra 2023-02-01 23:22:02 +01:00
parent 5107b08e1a
commit a7bef2e015
1 changed files with 59 additions and 19 deletions

View File

@ -1,4 +1,10 @@
#!/bin/bash #!/usr/bin/env bash
# ==============================================================================
# DESCRIPTION
#
#
# ==============================================================================
#verify run as root or sudo #verify run as root or sudo
if [[ $EUID -ne 0 ]]; then if [[ $EUID -ne 0 ]]; then
@ -8,6 +14,7 @@ fi
readonly OFFSET_OPTIONS=3 readonly OFFSET_OPTIONS=3
declare -a TARGETS declare -a TARGETS
NUM_HOSTS=0
show_ssh_hosts () { show_ssh_hosts () {
SSH_HOSTS_CONFIG_FILE="/root/.ssh/config" SSH_HOSTS_CONFIG_FILE="/root/.ssh/config"
@ -15,8 +22,8 @@ show_ssh_hosts () {
if [ -f $SSH_HOSTS_CONFIG_FILE ]; then if [ -f $SSH_HOSTS_CONFIG_FILE ]; then
TARGETS=($(cat $SSH_HOSTS_CONFIG_FILE | grep -E "^Host [^\*\*]" | sed "s/Host //")) TARGETS=($(cat $SSH_HOSTS_CONFIG_FILE | grep -E "^Host [^\*\*]" | sed "s/Host //"))
# get length of an array # get length of an array
length=${#TARGETS[@]} NUM_HOSTS=${#TARGETS[@]}
for (( j=0; j<length; j++ )); for (( j=0; j<NUM_HOSTS; j++ ));
do do
#printf "Current index %d with value %s\n" $j "${TARGETS[$j]}" #printf "Current index %d with value %s\n" $j "${TARGETS[$j]}"
echo "$((j+$OFFSET_OPTIONS))) ${TARGETS[$j]}" echo "$((j+$OFFSET_OPTIONS))) ${TARGETS[$j]}"
@ -26,20 +33,52 @@ show_ssh_hosts () {
fi fi
} }
is_root () { #is_root () {
echo "test root" #echo "test root"
echo "$(id -u)" #echo "$(id -u)"
return $(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
} }
remote_update () { remote_update () {
echo $1
ssh $1 << EOF ssh $1 << EOF
# date $(typeset -f has_sudo)
# hostname $(typeset -f elevate_cmd)
# cat /etc/resolv.conf elevate_cmd apt update -qq
$(typeset -f is_root) elevate_cmd apt upgrade -y
is_root elevate_cmd apt autoremove
EOF EOF
} }
@ -63,7 +102,6 @@ echo "0) Sortir de l'script"
echo "1) current host: $(hostname)" echo "1) current host: $(hostname)"
echo "2) custom host" echo "2) custom host"
show_ssh_hosts show_ssh_hosts
#echo -e "\n"
choosed=false choosed=false
while [ "$choosed" != "true" ] while [ "$choosed" != "true" ]
@ -80,16 +118,18 @@ if [ "$HOST" = "0" ]; then
finished=true finished=true
exit 0 exit 0
elif [ "$HOST" = "1" ]; then elif [ "$HOST" = "1" ]; then
echo -e "update now!\n" apt update -qq
apt upgrade -y
apt autoremove
elif [ "$HOST" = "2" ]; then elif [ "$HOST" = "2" ]; then
read -p 'Indica el host remot: ' CUSTOM read -p 'Indica el host remot: ' CUSTOM
echo $CUSTOM echo $CUSTOM
elif [ "$((HOST-OFFSET_OPTIONS))" -gt 7 ]; then elif [ "$((HOST-OFFSET_OPTIONS))" -lt $NUM_HOSTS ]; then
echo -e "Aquest número de host no existeix. Tornar a intentar-ho\n" index=$((HOST-OFFSET_OPTIONS))
else
index=$((HOST-OFFSET_OPTIONS))
echo "${TARGETS[$index]}" echo "${TARGETS[$index]}"
remote_update "${TARGETS[$index]}" remote_update "${TARGETS[$index]}"
else
echo -e "Aquest número de host no existeix. Tornar a intentar-ho\n"
fi fi
done done