Compare commits

..

No commits in common. "dev" and "master" have entirely different histories.
dev ... master

5 changed files with 43 additions and 201 deletions

4
.gitignore vendored
View File

@ -1,4 +0,0 @@
/.idea/
/send_files_bash.iml
/.gitignore
/src/

View File

@ -1,5 +1,3 @@
# garbage redo
# Experimental usage
Download the scripts **send_files.sh** and **distribute.sh**
@ -50,7 +48,6 @@ Place the files on the working structure (aka. script folder)
./distribute.sh
./send_files.sh
./files_to_send
./proxy_route
./users
#### files to send
@ -64,10 +61,6 @@ Place the files on the working structure (aka. script folder)
./user2/ <---- directory from the user
./user2/p <---- txt with password of the user
./_proxy_route1/ <---- Directory for the proxy route 'proxy_route1'
./_proxy_route1/u <---- txt with the username used to authenticate to 'proxy_route1'
./_proxy_route1/p <---- txt with password used to authenticate to 'proxy_route1'
```
#####

View File

@ -1,127 +1,52 @@
#!/bin/bash
## Author Oriol Filter
# Date 29/03/2022
# Date 16/02/2022
## Bring Shell to script folder
cd "$(dirname "$0")" || exit
## Find which user is the correct one
### VAR
export _USERS="$(cat ./users)"
export _PROXY_ROUTES="$(cat ./proxy_routes)"
_SSH_TIMOUT=2
### 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
sed '/^$/d' h | tee ./curr_h >/dev/null # remove empty lines
printf "Attempting to SSH\n"
if [[ "$_USERS" ]]
then
for username in $_USERS
for u in $_USERS
do
printf "\033[0;33m $u\n\033[0m"
rm ./${u}/h
touch ./${u}/h
for h in $(cat ./curr_h)
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";
printf "${h}: -->"
# sshpass -f "./${u}/p" ssh -q -o GSSAPIAuthentication=no -o StrictHostKeyChecking=no -T "${u}@${h}" 'echo true' &> /dev/null &&
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
sshpass -f "./${u}/p" ssh -q -o StrictHostKeyChecking=no -T "${u}@${h}" 'echo true' &> /dev/null &&
printf "${h}: \033[1;32mYES \033[0m\n" &&
printf "${h}\n" | tee -a "./${u}/h" > /dev/null ||
printf "${h}: \033[1;31mNo \033[0m\n" < "./${u}/p"
done
fi
diff --new-line-format="" --unchanged-line-format="" ./curr_h "${u}/h" > left_h
mv ./left_h ./curr_h
done
mv curr_h discards_h
# check if hosts found, if not, print skipping...
#printf "Proxy routes"
# Check if hosts left
printf "Distributed hosts\n"
for u in $_USERS
do
printf "\033[0;32m $u\n\033[0m"
awk '{ printf" %s\n",$0 }' < "./${u}/h"
done
# Stores results in ./.proxy_results
printf "" >.proxy_results
if [[ "$_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";
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 -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
fi
done
fi
mv .curr_h .discards_h
# Print Result
printf "Printing results:\n"
if [[ "$_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
printf "\n"
printf "\033[0;31mDISCARD HOSTS:${_RESET}\n"
awk '{ printf" %s\n",$0 }' < ./.discards_h
printf "\033[0;31mDISCARDS:\033[0m\n"
awk '{ printf" %s\n",$0 }' < ./discards_h
printf "Discards are stored in './discards_h'\n"
# Send files
bash ./send_files.sh

View File

@ -1,2 +0,0 @@
proxy_route1
proxy_route2

View File

@ -1,108 +1,38 @@
#!/bin/bash
## Author Oriol Filter
# Date 29/03/2022
# Date 10/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=7
### Main
printf "Sending files to /tmp\n"
if [[ "$_USERS" ]]
then
for username in $_USERS
do
printf "\033[0;33m ${username}${_RESET}\n"
### Main
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
for u in $_USERS
do
printf "\033[0;33m $u\033[0m\n"
# Copy files to proxy host
PROXY_IN_RESULTS=$(awk -F ';' '{print $2}' ./.proxy_results | sort | uniq)
if [[ "$PROXY_IN_RESULTS" ]]
then
for _proxy in $PROXY_IN_RESULTS
for h in $(cat "${u}/h")
do
printf ">> ${_YELLOW}${_proxy}:${_RESET}\n"
printf "" > "./_${_proxy}/result"
proxy_username="$(cat ./_${_proxy}/u)"
printf "\033[1;34m $h\033[0m\n"
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
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"
done
printf " -----------\n"
done
fi
done
printf "Sending files from proxy hosts\n"
# Copy files from proxy to remote
PROXY_SORTED_LINES=$(sort -t ';' -k2 ./.proxy_results)
if [[ "$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"
printf "\033[1;32mDONE \033[0m\n"