Hi All,
I'm new to the forum and to bash scripting. I did some stuff with VB.net, Batch, and VBScripting in the past, but because I shifted over to Linux, I am learning to script in Bash at this moment. So bear with me if I seem to script like a newbie, that's just because I am
OK, I wrote this script. It does the job, but it's rather slow. I get the server names, files, and password from separate text files. I place the servers and files into a list and loop them, but I configured only 2 local VM's at the moment and it runs rather slow already. I think I know why... It is logging into the server every time and then gets the file(s) I need. How do I overcome this? So, in my opinion, it just needs to login once and then gets the files off and after that move on to the other server.
Let me know what your thoughts are
Here it is:
#!/usr/bin/env bash
############################################ Global Declarations #################################################
home_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
dest_dir="/home/cornelis/config_files_properties"
ssh_user="root"
ssh_pass=$home_dir"/password_file" # Not needed on the script server
logfile="/tmp/config_files_properties.log"
############################################### Do the job #####################################################
## first thing to check if we are root, if not close
#
#if [ $(whoami) != 'root' ]; then
# echo "Must be root to run $0"
# exit 1;
#fi
# redirect all output to log file
exec &> >(while read -r line; do printf '%s %s\n' "$(date)" "$line"; done >> $logfile)
## check if destination folder exists and if not make it
if [[ ! -d "$dest_dir" ]]; then
mkdir -p $dest_dir
fi
# Deleting existing files in the destination folder (if empty send error to /dev/null)
# extra error handling just in case, before removing anything
cd $dest_dir
if [[ "$?" = "0" ]]; then
# empty folder
shopt -s dotglob && rm -r $dest_dir/* > /dev/null 2>&1
# get the files off the servers and place them in the destination folder in its own folder
index=0
while read line; do
server_array[index]="$line"
for server in ${server_array[*]}
do
# create the folders named per server
mkdir -p $dest_dir/$server
#find the correct files
index2=0
while read line2; do
file_array[index2]="$line2"
for file in ${file_array[*]}
do
#copy the files to the folders
sshpass -f $ssh_pass scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -rpqv $ssh_user@$server:$file $dest_dir/$server
done
done < $home_dir/files
done
done < $home_dir/test_server_names
# change the directory and run SED on the files and folders
cd $dest_dir || exit 1
find ./ -type f -print0 | xargs -0 sed -Ei 's/([^&]password=)([^\t "]*)/\1xxxxxx/gI; s/(password.*>).*(<)/\1xxxxxx\2/gI; s/(password" value=").[^"]*/\1xxxxxx/gI; s/=admin:.[^\t ]*/=admin:xxxxxx/g; /"Password">/ {n;N;s/([\t ]*).*(\n.*<\/)/\1xxxxxx\2/gI}'
#zip the folders into one zip file
zip -rq9 properties.zip *
else
echo "Cannot change directory to delete old files! Aborting! " 1>&2 >> $logfile
exit 1
fi
# open the folder with contents
xdg-open $dest_dir