From 534bc6fe656691e0e4ad4a3ec77a4c24a7b8b76b Mon Sep 17 00:00:00 2001 From: ofilter Date: Sun, 27 Mar 2022 06:01:32 +0200 Subject: [PATCH] proxy working --- distribute.sh | 114 ++++++++++++++++++++++---------------------------- send_files.sh | 91 +++++++++++++++++++++++++++++++++------- 2 files changed, 126 insertions(+), 79 deletions(-) diff --git a/distribute.sh b/distribute.sh index e15a647..e7b2465 100644 --- a/distribute.sh +++ b/distribute.sh @@ -1,7 +1,7 @@ #!/bin/bash ## Author Oriol Filter -# Date 16/02/2022 +# Date 27/03/2022 ## Find which user is the correct one @@ -9,7 +9,7 @@ ### VAR export _USERS="$(cat ./users)" export _PROXY_ROUTES="$(cat ./proxy_routes)" - +_SSH_TIMOUT=2 ### COLORS @@ -18,47 +18,49 @@ _GREEN="\033[1;32m" _RESET="\033[0m" _YELLOW="\033[0;33m" + ### Main sed '/^$/d' h | tee ./.curr_h >/dev/null # remove empty lines printf "Attempting to SSH\n" -#for username in $_USERS -#do -# printf "${_YELLOW} ${username}\n${_RESET}" -# printf "" > "./${username}/h" -# for hostname in $(cat ./.curr_h) -# do -# printf "${hostname}: -->" -# result="" -# sshpass -f "./${username}/p" ssh -q -o StrictHostKeyChecking=no -T "${username}@${hostname}" 'echo true' &> /dev/null && result="true"; -# -# if [[ "$result" ]] -# then -# printf "${hostname}\n" | tee -a "./${username}/h" > /dev/null -# printf "${hostname}: ${_GREEN}YES${_RESET}\n" -# else -# printf "${hostname}: ${_RED}No${_RESET}\n"; -# fi -# done -# diff --new-line-format="" --unchanged-line-format="" ./.curr_h "${username}/h" > .left_h -# mv ./.left_h ./.curr_h -#done +for username in $_USERS +do + printf "${_YELLOW} ${username}\n${_RESET}" + printf "" > "./${username}/h" + for hostname in $(cat ./.curr_h) + do + printf "${hostname}: -->" + result="" + sshpass -f "./${username}/p" ssh -q -o StrictHostKeyChecking=no -o ConnectTimeout=${_SSH_TIMOUT} -T "${username}@${hostname}" 'echo true' &> /dev/null && result="true"; + if [[ "$result" ]] + then + tee -a "./${username}/h" <<< "${hostname}" &> /dev/null + printf "${_GREEN}YES${_RESET}\n" + else + printf "${_RED}No${_RESET}\n"; + fi + done + diff --new-line-format="" --unchanged-line-format="" ./.curr_h "${username}/h" > ./._curr_h + mv ./._curr_h ./.curr_h +done +# check if hosts found, if not, print skipping... #printf "Proxy routes" # Check if hosts left + +# Stores results in ./.proxy_results printf "" >.proxy_results for proxy in $_PROXY_ROUTES do printf "${_YELLOW}>> Status proxy ${proxy}:${_RESET} " - printf "" > "./_${proxy}/result" proxy_username="$(cat ./_${proxy}/u)" result="" - sshpass -f "./${proxy_username}/p" ssh -q -o StrictHostKeyChecking=no -T "${proxy_username}@${proxy}" 'echo true' &> /dev/null && result="true"; + sshpass -f "./_${proxy}/p" ssh -q -o StrictHostKeyChecking=no -o ConnectTimeout=${_SSH_TIMOUT} -T "${proxy_username}@${proxy}" 'echo true' &> /dev/null && result="true"; if [[ ! "$result" ]] @@ -72,63 +74,47 @@ do do printf ">> ($proxy) --> ${username}@${hostname}: ${_RESET}" result="" - sshpass -f "./${_proxy}/p" ssh -q -o StrictHostKeyChecking=no -T "${proxy_username}@${proxy}" "sshpass -f /dev/stdin ssh -q -o StrictHostKeyChecking=no -T \"${username}@${hostname}\" \"echo true\" && echo true " < "./${username}/p" &> /dev/null && result="true"; + sshpass -f "./_${proxy}/p" ssh -q -o StrictHostKeyChecking=no -o ConnectTimeout=${_SSH_TIMOUT} -T "${proxy_username}@${proxy}" "sshpass -f /dev/stdin ssh -q -o StrictHostKeyChecking=no -o ConnectTimeout=${_SSH_TIMOUT} -T \"${username}@${hostname}\" \"echo true\" && echo true " &> /dev/null < "./${username}/p" && result="true"; if [[ "$result" ]] then printf "${_GREEN}YES${_RESET}\n" # hostname;proxy;username printf "${hostname};${proxy};${username}\n" | tee -a ./.proxy_results > /dev/null - diff --new-line-format="" --unchanged-line-format="" ./.curr_h <(echo $hostname) > .left_h - mv ./.left_h ./.curr_h -# awk -F ";" '{print $1}' .proxy_results + diff --new-line-format="" --unchanged-line-format="" ./.curr_h <(echo $hostname) > ./._curr_h + mv ./._curr_h ./.curr_h else printf "${_RED}No${_RESET}\n"; fi done done fi - - - - # for username in $_USERS - # do - # printf "${_YELLOW} ${username}\n${_RESET}" - # rm ./${username}/h - # touch ./${username}/h - # for hostname in $(cat ./.curr_h) - # do - # printf "${hostname}: -->" - # sshpass -f "./${username}/p" ssh -q -o StrictHostKeyChecking=no -T "${username}@${hostname}" 'echo true' &> /dev/null && - # printf "${hostname}: ${_GREEN}YES${_RESET}\n" && - # printf "${hostname}\n" | tee -a "./${username}/h" > /dev/null || - # printf "${hostname}: ${_RED}No${_RESET}\n" < "./${username}/p" - # done - # diff --new-line-format="" --unchanged-line-format="" ./.curr_h "${username}/h" > .left_h - # mv ./.left_h ./.curr_h - # done - done +# Print Result +printf "Printing results:\n" +mv .curr_h .discards_h +for username in $_USERS +do + printf ">> \033[0;32m ${username}\n${_RESET}" + awk '{ printf" %s\n",$0 }' < "./${username}/h" +done - - - - - -#printf "Distributed hosts\n" -#mv .curr_h .discards_h +printf ">> Printing results from proxies\n" +awk -F ';' '{printf "\t%s -->\t%s (%s)",$2,$1,$3}' ./.proxy_results | col #for username in $_USERS #do -# printf "\033[0;32m ${username}\n${_RESET}" -# awk '{ printf" %s\n",$0 }' < "./${username}/h" +# #done -# -#printf "\033[0;31mDISCARDS:${_RESET}\n" -#awk '{ printf" %s\n",$0 }' < ./.discards_h -# -#printf "Discards are stored in './discards_h'\n" -# + +printf "\n" +printf "\033[0;31mDISCARD HOSTS:${_RESET}\n" +awk '{ printf" %s\n",$0 }' < ./.discards_h + +printf "Discards are stored in './discards_h'\n" + + +# Send files #bash ./send_files.sh diff --git a/send_files.sh b/send_files.sh index c7dd411..7f3b54d 100644 --- a/send_files.sh +++ b/send_files.sh @@ -1,41 +1,102 @@ #!/bin/bash ## Author Oriol Filter -# Date 10/03/2022 +# Date 27/03/2022 ## Send scripts/files to the servers +### COLORS + +_RED="\033[1;31m" +_GREEN="\033[1;32m" +_RESET="\033[0m" +_YELLOW="\033[0;33m" + ### VAR -# check if _users is empty -# _USERS="ibmuser bs2cloud" _USERS="$(cat ./users)" - -#_USERS="$_USERS" _FILES="$(cat ./files_to_send)" +_SSH_TIMOUT=5 -printf "Sending files to /tmp\n" ### Main -for u in $_USERS +printf "Sending files to /tmp\n" +for username in $_USERS do - printf "\033[0;33m $u\033[0m\n" + printf "\033[0;33m ${username}${_RESET}\n" - for h in $(cat "${u}/h") + for hostname in $(cat "${username}/h") do - printf "\033[1;34m $h\033[0m\n" + printf "\033[1;34m ${hostname}${_RESET}\n" for _F in $_FILES do - sshpass -f "./${u}/p" scp -q -o StrictHostKeyChecking=no "$_F" "${u}@${h}:/tmp/" && - printf "\t${_F}: \033[1;32mYES \033[0m\n" || - printf "\t${_F}: \033[1;31mNo \033[0m\n" + result="" + printf "\t${_F}: " + sshpass -f "./${username}/p" scp -q -o StrictHostKeyChecking=no -o ConnectTimeout=${_SSH_TIMOUT} "$_F" "${username}@${hostname}:/tmp/" && result="true" + if [[ "$result" ]] + then + printf "${_GREEN}YES${_RESET}\n" + else + printf "${_RED}No${_RESET}\n"; + fi done printf " -----------\n" done done -# Proxy + +# Copy files to proxy host +PROXY_IN_RESLTS=$(awk -F ';' '{print $2}' ./.proxy_results | sort | uniq) + +for _proxy in $PROXY_IN_RESLTS +do + printf ">> ${_YELLOW}${_proxy}:${_RESET}\n" + printf "" > "./_${_proxy}/result" + proxy_username="$(cat ./_${_proxy}/u)" + for _F in $_FILES + do + result="" + sshpass -f "./${_proxy}/p" scp -q -o StrictHostKeyChecking=no -o ConnectTimeout=${_SSH_TIMOUT} "$_F" "${proxy_username}@${_proxy}:/tmp" && result="true"; + printf "\t${_F}: " + if [[ "$result" ]] + then + printf "${_GREEN}YES${_RESET}\n" + else + printf "${_RED}No${_RESET}\n"; + fi + done + printf " -----------\n" +done -printf "\033[1;32mDONE \033[0m\n" + +printf "Sending files from proxy hosts\n" +# Copy files from proxy to remote +PROXY_SORTED_LINES=$(sort -t ';' -k2 ./.proxy_results) + +#for _LINE in $PROXY_SORTED_LINES +while IFS= read -r _LINE +do + _proxy=$(awk -F ';' '{print $2}' <<< "$_LINE") + hostname=$(awk -F ';' '{print $1}' <<< "$_LINE") + username=$(awk -F ';' '{print $3}' <<< "$_LINE") + proxy_username=$(cat "./_${_proxy}/u") + printf "%s -->\t%s\n" "$_proxy" "$hostname" + for _F in $_FILES + do + result="" + cat "./${username}/p" | sshpass -f "./${_proxy}/p" ssh -q -o StrictHostKeyChecking=no -o ConnectTimeout=${_SSH_TIMOUT} -T "${proxy_username}@${_proxy}" "sshpass -f /dev/stdin scp -q -o StrictHostKeyChecking=no -o ConnectTimeout=${_SSH_TIMOUT} /tmp/${_F} \"${username}@${hostname}\" && echo true" &> /dev/null && result="true"; + printf "\t${_F}: " + if [[ "$result" ]] + then + printf "${_GREEN}YES${_RESET}\n" + else + printf "${_RED}No${_RESET}\n"; + fi + done + printf " -----------\n" +done <<< "$PROXY_SORTED_LINES" + + +printf "${_GREEN}DONE${_RESET}\n"