diff --git a/distribute.sh b/distribute.sh index 4b5a619..7401dc1 100644 --- a/distribute.sh +++ b/distribute.sh @@ -1,10 +1,12 @@ #!/bin/bash ## Author Oriol Filter -# Date 27/03/2022 +# Date 29/03/2022 -## Find which user is the correct one +## Bring Shell to script folder +cd "$(dirname "$0")" || exit + ### VAR export _USERS="$(cat ./users)" @@ -25,27 +27,30 @@ 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) +if [[ -z "$_USERS" ]] +then + for username in $_USERS 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"; + 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 + 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 - diff --new-line-format="" --unchanged-line-format="" ./.curr_h "${username}/h" > ./._curr_h - mv ./._curr_h ./.curr_h -done +fi # check if hosts found, if not, print skipping... #printf "Proxy routes" @@ -54,60 +59,62 @@ done # Stores results in ./.proxy_results printf "" >.proxy_results -for proxy in $_PROXY_ROUTES -do - printf "${_YELLOW}>> Status proxy ${proxy}:${_RESET} " - proxy_username="$(cat ./_${proxy}/u)" +if [[ -z "$_PROXY_ROUTES" ]] +then + for proxy in $_PROXY_ROUTES + do + printf "${_YELLOW}>> Status proxy ${proxy}:${_RESET} " + proxy_username="$(cat ./_${proxy}/u)" - result="" - sshpass -f "./_${proxy}/p" ssh -q -o StrictHostKeyChecking=no -o ConnectTimeout=${_SSH_TIMOUT} -T "${proxy_username}@${proxy}" 'echo true' &> /dev/null && result="true"; + result="" + 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" ]] - then - printf "${_RED}Cannot establish connection with the proxy, skipping ...${_RESET}\n"; - else - printf "${_GREEN}OK${_RESET}\n" - for username in $_USERS - do - for hostname in $(cat ./.curr_h) + if [[ ! "$result" ]] + then + printf "${_RED}Cannot establish connection with the proxy, skipping ...${_RESET}\n"; + else + printf "${_GREEN}OK${_RESET}\n" + for username in $_USERS do - printf ">> ($proxy) --> ${username}@${hostname}: ${_RESET}" - result="" - 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) > ./._curr_h - mv ./._curr_h ./.curr_h - else - printf "${_RED}No${_RESET}\n"; - fi + for hostname in $(cat ./.curr_h) + do + printf ">> ($proxy) --> ${username}@${hostname}: ${_RESET}" + result="" + 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) > ./._curr_h + mv ./._curr_h ./.curr_h + else + printf "${_RED}No${_RESET}\n"; + fi + done done - done - fi -done - + fi + done +fi +mv .curr_h .discards_h # 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 +if [[ -z "$_USERS" ]] +then + for username in $_USERS + do + printf ">> \033[0;32m ${username}\n${_RESET}" + awk '{ printf" %s\n",$0 }' < "./${username}/h" + done +fi 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 -# -#done + printf "\n" printf "\033[0;31mDISCARD HOSTS:${_RESET}\n" diff --git a/send_files.sh b/send_files.sh index 7f3b54d..7f0ae18 100644 --- a/send_files.sh +++ b/send_files.sh @@ -1,7 +1,8 @@ #!/bin/bash ## Author Oriol Filter -# Date 27/03/2022 +# Date 29/03/2022 + ## Send scripts/files to the servers @@ -22,18 +23,47 @@ _SSH_TIMOUT=5 ### Main printf "Sending files to /tmp\n" -for username in $_USERS -do - printf "\033[0;33m ${username}${_RESET}\n" - for hostname in $(cat "${username}/h") +if [[ -z "$_USERS" ]] +then + for username in $_USERS do - printf "\033[1;34m ${hostname}${_RESET}\n" + printf "\033[0;33m ${username}${_RESET}\n" + + for hostname in $(cat "${username}/h") + do + printf "\033[1;34m ${hostname}${_RESET}\n" + for _F in $_FILES + do + 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 +fi + +# Copy files to proxy host +PROXY_IN_RESULTS=$(awk -F ';' '{print $2}' ./.proxy_results | sort | uniq) +if [[ -z "$PROXY_IN_RESULTS" ]] +then + for _proxy in $PROXY_IN_RESULTS + 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}: " - 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" @@ -43,60 +73,36 @@ do done printf " -----------\n" done -done - - -# 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 - +fi 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" - +if [[ -z "$PROXY_SORTED_LINES" ]] +then + 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" +fi printf "${_GREEN}DONE${_RESET}\n"