RemotesRun/run_all.sh

90 lines
3.4 KiB
Bash
Raw Normal View History

#!/bin/bash -i
2022-01-08 13:07:34 +00:00
# ____ _ ____
# | _ \ ___ _ __ ___ ___ | |_ ___ ___| _ \ _ _ _ __
# | |_) / _ \ '_ ` _ \ / _ \| __/ _ \/ __| |_) | | | | '_ \
# | _ < __/ | | | | | (_) | || __/\__ \ _ <| |_| | | | |
# |_| \_\___|_| |_| |_|\___/ \__\___||___/_| \_\\__,_|_| |_|
#
# Run several scripts on remote servers automatically.
# Copyright (C) 2022 Ad5001
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
echo ""
2024-09-29 22:03:47 +00:00
set +o history
2022-01-08 13:38:55 +00:00
# Cd into the directory of the library
script="$(readlink $0)"
if [ -z "$script" ]; then script="$0"; fi
cd "$(dirname "$script")"
2022-01-08 13:07:34 +00:00
# Importing common functions.
. ./libraries/common.sh
# Set remotes to remote folder to "remotes folder if not set.
if [ -z "$REMOTES" ]; then
REMOTES="remotes"
fi
for folder in "$REMOTES"/*; do
host=$(basename "$folder")
# Check for powerline.
if [ -z "$(which powerline)" ]; then
prompt="${L_GREEN_BG}${BLACK_FG} Password for ${RESET_INVERT}${ORANGE_BG}$host ${RESET} 🔑 > "
2022-01-08 13:07:34 +00:00
else
prompt="${L_GREEN_BG}${BLACK_FG} Password for ${ORANGE_FG}${INVERT}${RESET_INVERT}${ORANGE_BG}${YELLOW_FG}$host ${GRAY_FG}${INVERT}${RESET_INVERT}${GRAY_BG}${BLACK_FG} 🔑 ${INVERT}${RESET}"
2022-01-08 13:07:34 +00:00
fi
echo -e -n "$prompt"
2022-01-08 13:07:34 +00:00
read -s pswd
echo -e "\n"
dependencies="libraries/common.sh"
full_script=""
2023-03-22 12:24:58 +00:00
port="$(cat $folder/port)"
2022-01-08 13:07:34 +00:00
# Executing all necessary scripts
if [[ $pswd != "skip" ]]; then
for script in $folder/*.sh; do
scriptname=$(basename $script)
scriptname=${scriptname//_/ }
scriptname=${scriptname::-3}
# Get dependencies from file.
dependencies="$dependencies $(sed -n '2p' $script | grep "# Requires: " | cut -d" " -f3-)"
# Create the full script string using the box command to signal the new script
full_script="${full_script}box 'Executing script $scriptname...'
$(cat "$script" | awk '$1 != "#" && $1 != ""')
"
2022-01-08 13:07:34 +00:00
done
# Removing duplicate dependencies
dependencies="$(echo $dependencies | awk '!a[$0]++')"
# Finish the script
full_script="$(cat $dependencies | awk '$1 != "#" && $1 != ""')
$full_script"
# Save temporary askpass for the password.
echo -e '#!/bin/bash\necho "'$pswd'"' > tmp.sh
chmod +x tmp.sh
export SSH_ASKPASS="$(pwd)/tmp.sh"
export SSH_ASKPASS_REQUIRE="force"
# Transmit the script
2023-03-22 12:24:58 +00:00
echo "$full_script" | ssh -q $host -p $port "tee run.sh" 1>/dev/null
info "Transmitted script"
# Run and remove it.
2024-09-29 22:03:47 +00:00
ssh $host -p $port PASSWORD="$pswd" "bash run.sh && rm run.sh"
2023-03-22 12:24:58 +00:00
## Interactive shell does not work.
#ssh -t $host PASSWORD="$pswd" "bash -i run.sh && rm run.sh"
rm tmp.sh
2022-01-08 13:07:34 +00:00
else
box "Skipping ${host}..."
fi
done