Adding interactivity for all scripts, running all scripts in 1 ssh session.

This commit is contained in:
Ad5001 2022-05-07 23:02:40 +02:00
parent 20b789268f
commit fdc09b75f7
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
2 changed files with 34 additions and 10 deletions

View file

@ -36,6 +36,8 @@ RESET_INVERT="\e[27m"
GRAY_FG="\e[90m"
# Sets the forground color to green.
GREEN_FG="\e[92m"
# Sets the forground color to yellow
YELLOW_FG="\e[38;5;220m"
# Sets the forground color to blue.
BLUE_FG="\e[94m"
@ -51,7 +53,7 @@ BLACK_FG="\e[30m"
# Sets the forground color to light green.
L_GREEN_FG="\e[32m"
# Sets the forground color to orange.
ORANGE_FG="\e[33m"
ORANGE_FG="\e[38;5;166m" #"\e[33m"
# Sets the forground color to red.
RED_FG="\e[38;5;204m"
@ -60,7 +62,7 @@ BLACK_BG="\e[40m"
# Sets the background color to light green.
L_GREEN_BG="\e[42m"
# Sets the background color to orange.
ORANGE_BG="\e[43m"
ORANGE_BG="\e[48;5;166m"
# Sets the background color to red.
RED_FG="\e[48;5;204m"
@ -68,8 +70,10 @@ RED_FG="\e[48;5;204m"
# Signature: (<string command>) -> string
run_as_root() {
echo "$PASSWORD" | sudo -p "" -i -S bash -c "$@"
#sudo bash -c "$@"
}
# Simple info blue command.
# Signature: (<string message>) -> string
info() {

View file

@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/bash -i
# ____ _ ____
# | _ \ ___ _ __ ___ ___ | |_ ___ ___| _ \ _ _ _ __
# | |_) / _ \ '_ ` _ \ / _ \| __/ _ \/ __| |_) | | | | '_ \
@ -39,24 +39,44 @@ for folder in "$REMOTES"/*; do
host=$(basename "$folder")
# Check for powerline.
if [ -z "$(which powerline)" ]; then
echo -e -n "${L_GREEN_BG}${BLACK_FG} Password for ${RESET_INVERT}${ORANGE_BG}$host ${RESET} 🔑 > "
prompt="${L_GREEN_BG}${BLACK_FG} Password for ${RESET_INVERT}${ORANGE_BG}$host ${RESET} 🔑 > "
else
echo -e -n "${L_GREEN_BG}${BLACK_FG} Password for ${ORANGE_FG}${INVERT}${RESET_INVERT}${ORANGE_BG}${BLACK_FG}${host} ${GRAY_FG}${INVERT}${RESET_INVERT}${GRAY_BG}${BLACK_FG} 🔑 ${INVERT}${RESET}"
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}"
fi
echo -e -n "$prompt"
read -s pswd
echo -e "\n"
dependencies="libraries/common.sh"
full_script=""
# Executing all necessary scripts
if [[ $pswd != "skip" ]]; then
for script in $folder/*.sh; do
scriptname=$(basename $script)
scriptname=${scriptname//_/ }
scriptname=${scriptname::-3}
box "Executing script $scriptname..."
# Taking dependencies into account.
dependencies="libraries/common.sh $(sed -n '2p' $script | grep "# Requires: " | cut -d" " -f3-)" # Get dependencies from file.
# Cat dependencies, remove all empty or comment lines, and pipe them into sshed bash.
cat $dependencies $script | awk '$1 != "#" && $1 != ""' | sshpass -p "$pswd" -P "pass" ssh $host PASSWORD="$pswd" "bash -s"
# 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 != ""')
"
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
echo "$full_script" | ssh -q $host "tee run.sh" 1>/dev/null
info "Transmitted script"
# Run and remove it.
ssh -t $host PASSWORD="$pswd" "bash -i run.sh && rm run.sh"
rm tmp.sh
else
box "Skipping ${host}..."
fi