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

View file

@ -1,4 +1,4 @@
#!/bin/bash #!/bin/bash -i
# ____ _ ____ # ____ _ ____
# | _ \ ___ _ __ ___ ___ | |_ ___ ___| _ \ _ _ _ __ # | _ \ ___ _ __ ___ ___ | |_ ___ ___| _ \ _ _ _ __
# | |_) / _ \ '_ ` _ \ / _ \| __/ _ \/ __| |_) | | | | '_ \ # | |_) / _ \ '_ ` _ \ / _ \| __/ _ \/ __| |_) | | | | '_ \
@ -39,24 +39,44 @@ for folder in "$REMOTES"/*; do
host=$(basename "$folder") host=$(basename "$folder")
# Check for powerline. # Check for powerline.
if [ -z "$(which powerline)" ]; then 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 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 fi
echo -e -n "$prompt"
read -s pswd read -s pswd
echo -e "\n" echo -e "\n"
dependencies="libraries/common.sh"
full_script=""
# Executing all necessary scripts # Executing all necessary scripts
if [[ $pswd != "skip" ]]; then if [[ $pswd != "skip" ]]; then
for script in $folder/*.sh; do for script in $folder/*.sh; do
scriptname=$(basename $script) scriptname=$(basename $script)
scriptname=${scriptname//_/ } scriptname=${scriptname//_/ }
scriptname=${scriptname::-3} scriptname=${scriptname::-3}
box "Executing script $scriptname..." # Get dependencies from file.
# Taking dependencies into account. dependencies="$dependencies $(sed -n '2p' $script | grep "# Requires: " | cut -d" " -f3-)"
dependencies="libraries/common.sh $(sed -n '2p' $script | grep "# Requires: " | cut -d" " -f3-)" # Get dependencies from file. # Create the full script string using the box command to signal the new script
# Cat dependencies, remove all empty or comment lines, and pipe them into sshed bash. full_script="${full_script}box 'Executing script $scriptname...'
cat $dependencies $script | awk '$1 != "#" && $1 != ""' | sshpass -p "$pswd" -P "pass" ssh $host PASSWORD="$pswd" "bash -s" $(cat "$script" | awk '$1 != "#" && $1 != ""')
"
done 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 else
box "Skipping ${host}..." box "Skipping ${host}..."
fi fi