From fdc09b75f77a4b5276bcc413fdb91c211468ce30 Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Sat, 7 May 2022 23:02:40 +0200 Subject: [PATCH] Adding interactivity for all scripts, running all scripts in 1 ssh session. --- libraries/common.sh | 8 ++++++-- run_all.sh | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/libraries/common.sh b/libraries/common.sh index 17ab904..c5a023f 100644 --- a/libraries/common.sh +++ b/libraries/common.sh @@ -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 run_as_root() { echo "$PASSWORD" | sudo -p "" -i -S bash -c "$@" + #sudo bash -c "$@" } + # Simple info blue command. # Signature: () -> string info() { diff --git a/run_all.sh b/run_all.sh index 09934f7..a069519 100755 --- a/run_all.sh +++ b/run_all.sh @@ -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