Send_files_bash/distribute.sh
ofilter 974a7d3f38 proxy working
left: print
send files
2022-03-25 06:40:38 +01:00

135 lines
3.7 KiB
Bash

#!/bin/bash
## Author Oriol Filter
# Date 16/02/2022
## Find which user is the correct one
### VAR
export _USERS="$(cat ./users)"
export _PROXY_ROUTES="$(cat ./proxy_routes)"
### COLORS
_RED="\033[1;31m"
_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
#printf "Proxy routes"
# Check if hosts left
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";
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)
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";
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
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
#printf "Distributed hosts\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 "\033[0;31mDISCARDS:${_RESET}\n"
#awk '{ printf" %s\n",$0 }' < ./.discards_h
#
#printf "Discards are stored in './discards_h'\n"
#
#bash ./send_files.sh