Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
81e389e6ba | ||
![]() |
97ebf895bb | ||
![]() |
d03844fdc9 | ||
![]() |
bd6176973e | ||
![]() |
1dfa28a0f4 | ||
![]() |
534bc6fe65 | ||
![]() |
974a7d3f38 | ||
![]() |
e5c862378b |
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
/.idea/
|
||||
/send_files_bash.iml
|
||||
/.gitignore
|
||||
/src/
|
@ -1,3 +1,5 @@
|
||||
# garbage redo
|
||||
|
||||
# Experimental usage
|
||||
|
||||
Download the scripts **send_files.sh** and **distribute.sh**
|
||||
@ -48,6 +50,7 @@ Place the files on the working structure (aka. script folder)
|
||||
./distribute.sh
|
||||
./send_files.sh
|
||||
./files_to_send
|
||||
./proxy_route
|
||||
./users
|
||||
|
||||
#### files to send
|
||||
@ -61,6 +64,10 @@ 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'
|
||||
```
|
||||
|
||||
#####
|
||||
|
129
distribute.sh
129
distribute.sh
@ -1,52 +1,127 @@
|
||||
#!/bin/bash
|
||||
|
||||
## Author Oriol Filter
|
||||
# Date 16/02/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)"
|
||||
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"
|
||||
|
||||
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)
|
||||
if [[ "$_USERS" ]]
|
||||
then
|
||||
for username in $_USERS
|
||||
do
|
||||
printf "${h}: -->"
|
||||
# sshpass -f "./${u}/p" ssh -q -o GSSAPIAuthentication=no -o StrictHostKeyChecking=no -T "${u}@${h}" 'echo true' &> /dev/null &&
|
||||
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";
|
||||
|
||||
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"
|
||||
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 "${u}/h" > left_h
|
||||
mv ./left_h ./curr_h
|
||||
done
|
||||
mv curr_h discards_h
|
||||
fi
|
||||
|
||||
printf "Distributed hosts\n"
|
||||
# check if hosts found, if not, print skipping...
|
||||
#printf "Proxy routes"
|
||||
# Check if hosts left
|
||||
|
||||
for u in $_USERS
|
||||
do
|
||||
printf "\033[0;32m $u\n\033[0m"
|
||||
awk '{ printf" %s\n",$0 }' < "./${u}/h"
|
||||
done
|
||||
|
||||
printf "\033[0;31mDISCARDS:\033[0m\n"
|
||||
awk '{ printf" %s\n",$0 }' < ./discards_h
|
||||
# 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 "Discards are stored in './discards_h'\n"
|
||||
|
||||
|
||||
# Send files
|
||||
bash ./send_files.sh
|
||||
|
2
proxy_routes
Normal file
2
proxy_routes
Normal file
@ -0,0 +1,2 @@
|
||||
proxy_route1
|
||||
proxy_route2
|
102
send_files.sh
102
send_files.sh
@ -1,38 +1,108 @@
|
||||
#!/bin/bash
|
||||
|
||||
## Author Oriol Filter
|
||||
# Date 10/03/2022
|
||||
# Date 29/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
|
||||
|
||||
printf "Sending files to /tmp\n"
|
||||
|
||||
### Main
|
||||
|
||||
for u in $_USERS
|
||||
do
|
||||
printf "\033[0;33m $u\033[0m\n"
|
||||
printf "Sending files to /tmp\n"
|
||||
|
||||
for h in $(cat "${u}/h")
|
||||
if [[ "$_USERS" ]]
|
||||
then
|
||||
for username in $_USERS
|
||||
do
|
||||
printf "\033[1;34m $h\033[0m\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 [[ "$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
|
||||
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=""
|
||||
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
|
||||
done
|
||||
fi
|
||||
|
||||
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)
|
||||
|
||||
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"
|
||||
|
Loading…
x
Reference in New Issue
Block a user