925 lines
39 KiB
Bash
Executable File
925 lines
39 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
##############################################################################
|
|
# ks-upr (ks-tools) - Upload recursively file(s) to server with scp or rsync #
|
|
# Date: 01-10-2023 #
|
|
# Author: q3aql #
|
|
# Contact: q3aql@duck.com #
|
|
##############################################################################
|
|
VERSION="8.5-dev"
|
|
M_DATE="011023"
|
|
|
|
# Global parameters.
|
|
dirTemp="/tmp"
|
|
listCompTemp="ks-tools.list-full"
|
|
listCompTempTest="ks-tools.list-full.test"
|
|
listTemp="ks-tools.list"
|
|
dirConfig="${HOME}/.ks-tools"
|
|
ksToolsTempFolder="/tmp/ks-tools"
|
|
|
|
# Check cygwin alias (for Windows)
|
|
if [ -f "/usr/bin/cygwin-alias.sh" ] ; then
|
|
shopt -s expand_aliases
|
|
cygwin="yes"
|
|
source "/usr/bin/cygwin-alias.sh"
|
|
else
|
|
cygwin="no"
|
|
fi
|
|
|
|
# Set the correct permissions to the pem file
|
|
if [ -f ${dirConfig}/connect.pem ] ; then
|
|
chmod 600 ${dirConfig}/connect.pem
|
|
fi
|
|
|
|
# Function to remove spaces and symbols
|
|
# Syntax: convertText "<text>"
|
|
function convertText() {
|
|
wordToConvert=${1}
|
|
ksToolsSedFile="${ksToolsTempFolder}/ks-tools-${RANDOM}.txt"
|
|
mkdir -p ${ksToolsTempFolder} && chmod 777 -R ${ksToolsTempFolder} 2> /dev/null
|
|
echo "${wordToConvert}" > ${ksToolsSedFile}
|
|
# Borrar espacios
|
|
sed -i 's/ /_/g' "${ksToolsSedFile}" &> /dev/null
|
|
# Borrar simbolos
|
|
symbolsList="[ ] @ { } | \ / ~ # $ % & ? ¿ = ( ) < > ! ¡"
|
|
for findSymbol in ${symbolsList} ; do
|
|
sed -i "s/${findSymbol}//g" "${ksToolsSedFile}" &> /dev/null
|
|
done
|
|
# Borrar el resto de simbolos
|
|
sed -i 's/*//g' "${ksToolsSedFile}" &> /dev/null
|
|
sed -i 's/"//g' "${ksToolsSedFile}" &> /dev/null
|
|
sed -i "s/^//g" "${ksToolsSedFile}" &> /dev/null
|
|
# Cambiar algunos simbolos
|
|
sed -i 's/+/_/g' "${ksToolsSedFile}" &> /dev/null
|
|
sed -i 's/:/-/g' "${ksToolsSedFile}" &> /dev/null
|
|
sed -i 's/;/-/g' "${ksToolsSedFile}" &> /dev/null
|
|
# Mostrar texto convertido
|
|
wordToConvert=$(cat ${ksToolsSedFile})
|
|
echo ${wordToConvert}
|
|
}
|
|
|
|
# Function to list the files of a directory.
|
|
# Syntax: listArchives <directory>
|
|
function listArchives() {
|
|
fail=1
|
|
formatFiles=""
|
|
videoFiles="mp4 avi mpg mpeg mov wmv mkv ogv webm rm flv vob"
|
|
formatFiles="${formatFiles} ${videoFiles}"
|
|
audioFiles="mp3 wma ogg wav flac midi acc oga opus mp2 ra m4a mka"
|
|
formatFiles="${formatFiles} ${audioFiles}"
|
|
compressFiles="zip rar 7z bz2 gz xz tar bzip2 gzip"
|
|
formatFiles="${formatFiles} ${compressFiles}"
|
|
open_libreOfficeFiles="odt ods odp odb odg"
|
|
formatFiles="${formatFiles} ${open_libreOfficeFiles}"
|
|
OfficeFiles="doc docx dotx dot docm dic thmx xlsx ppt pps pst mbd accdb msg"
|
|
formatFiles="${formatFiles} ${OfficeFiles}"
|
|
documentFiles="txt pdf rtf html php xml json css"
|
|
formatFiles="${formatFiles} ${documentFiles}"
|
|
pictureFiles="png jpg jpeg gif bmp raw xcf ppm pnm tiff ico tga pbm pgm svg psd"
|
|
formatFiles="${formatFiles} ${pictureFiles}"
|
|
executableFiles="exe msi run bin elf dmg deb rpm tgz ebuild flatpakref snap sh bash cmd bat AppImage jar"
|
|
formatFiles="${formatFiles} ${executableFiles}"
|
|
otherFiles="desktop lnk blend db c cpp cxx cc py rb dll so link network service ko list java"
|
|
formatFiles="${formatFiles} ${otherFiles}"
|
|
# Rename files
|
|
cd "${1}"
|
|
mkdir -p ${ksToolsTempFolder}
|
|
ls -1 > ${ksToolsTempFolder}/rename-files.txt
|
|
count=1
|
|
sizeFile=$(cat ${ksToolsTempFolder}/rename-files.txt | wc -l)
|
|
while [ ${count} -le ${sizeFile} ] ; do
|
|
fileToRename=$(cat ${ksToolsTempFolder}/rename-files.txt | head -${count} | tail -1)
|
|
fileRenamed=$(convertText "${fileToRename}")
|
|
if [ "${fileRenamed}" == "${fileToRename}" ] ; then
|
|
echo "null" > /dev/null
|
|
else
|
|
mv "${fileToRename}" "${fileRenamed}"
|
|
fi
|
|
count=$(expr $count + 1)
|
|
done
|
|
# Scan audio files
|
|
rm -rf ${dirTemp}/${listTemp}
|
|
rm -rf ${dirTemp}/${listCompTemp}
|
|
for format in ${formatFiles} ; do
|
|
find "${1}"/*.${format} &> ${dirTemp}/${listCompTempTest}
|
|
if [ $? -ne 0 ] ; then
|
|
echo "null" > /dev/null
|
|
else
|
|
find "${1}"/*.${format} &>> ${dirTemp}/${listCompTemp}
|
|
echo "+ File(s) in .${format} format found!"
|
|
cd "${1}" && ls -1 *.${format} &>> ${dirTemp}/${listTemp}
|
|
fail=0
|
|
fi
|
|
done
|
|
if [ ${fail} -eq 1 ] ; then
|
|
echo "+ No common file(s) found!"
|
|
echo ""
|
|
exit
|
|
else
|
|
echo ""
|
|
fi
|
|
}
|
|
|
|
# Function to count the found files.
|
|
function countArchives() {
|
|
totalArchives=$(cat ${dirTemp}/${listCompTemp} | wc -l)
|
|
echo ${totalArchives}
|
|
}
|
|
|
|
# Function to show files with spaces.
|
|
# Syntax: showFileWithSpace <file number>
|
|
function showFileWithSpace() {
|
|
FileName=$(cat ${dirTemp}/${listTemp} | head -${1} | tail -1)
|
|
FileNoExtension=$(echo $FileName | cut -d "." -f 1)
|
|
echo $FileNoExtension > ${dirTemp}/name.tmp
|
|
sed -i 's/_/ /g' ${dirTemp}/name.tmp
|
|
DisplayName=$(cat ${dirTemp}/name.tmp)
|
|
rm -rf ${dirTemp}/name.tmp
|
|
echo ${DisplayName}
|
|
}
|
|
|
|
# Function to show the name of the file.
|
|
# Syntax: showFile <file number>
|
|
function showFile() {
|
|
archive=$(cat ${dirTemp}/${listTemp} | head -${1} | tail -1)
|
|
echo ${archive}
|
|
}
|
|
|
|
# Function to send file to server.
|
|
# Syntax: sendFile <password> <file path> <user> <server> <server path> [file name]
|
|
function sendFile() {
|
|
toolSelected=$(cat ${dirConfig}/ks-upload-tool)
|
|
portSelected=$(cat ${dirConfig}/ks-upload-port)
|
|
if [ "${cygwin}" == "yes" ] ; then
|
|
if [ "${toolSelected}" == "rsync" ] ; then
|
|
echo ""
|
|
echo "+ Syncing folder ${2} to ${5} (${4})"
|
|
/usr/bin/rsync --progress -azL --rsh="/usr/bin/sshpass -p ${1} /usr/bin/ssh -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} -l ${3}" ${2}/ ${4}:${5}/ 2> /dev/null
|
|
/usr/bin/sshpass -p ${1} /usr/bin/ssh -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} rm -rf ${5}/.${6}.* &> /dev/null
|
|
elif [ "${toolSelected}" == "rsync-e" ] ; then
|
|
keyscan_host=$(cat ~/.ssh/known_hosts | grep ${4})
|
|
if [ -z "${keyscan_host}" ] ; then
|
|
/usr/bin/ssh-keyscan -p ${portSelected} ${4} >> ~/.ssh/known_hosts
|
|
fi
|
|
# Sync files using rsync with expect
|
|
/usr/bin/expect <<EOF
|
|
spawn /usr/bin/rsync --progress -rlDzL --port=${portSelected} ${2}/ ${3}@${4}:${5}/
|
|
set timeout -1
|
|
expect "*?assword:*"
|
|
send "${1}\n"
|
|
expect eof
|
|
EOF
|
|
OUTPUT=$?
|
|
elif [ "${toolSelected}" == "scp-e" ] ; then
|
|
# Sync files using scp with expect
|
|
/usr/bin/expect <<EOF
|
|
spawn /usr/bin/scp -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} -r ${2}/ ${3}@${4}:${5}/
|
|
set timeout -1
|
|
expect "*?assword:*"
|
|
send "${1}\n"
|
|
expect eof
|
|
EOF
|
|
OUTPUT=$?
|
|
elif [ "${toolSelected}" == "rsync-rsa" ] ; then
|
|
echo ""
|
|
echo "+ Syncing folder ${2} to ${5} (${4})"
|
|
/usr/bin/rsync --progress -azL --rsh="/usr/bin/sshpass -p ${1} /usr/bin/ssh -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} -l ${3}" ${2}/ ${4}:${5}/ 2> /dev/null
|
|
/usr/bin/sshpass -p ${1} /usr/bin/ssh -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} rm -rf ${5}/.${6}.* &> /dev/null
|
|
elif [ "${toolSelected}" == "scp-rsa" ] ; then
|
|
echo ""
|
|
echo "+ Sending files from ${2} to ${5} (${4})"
|
|
/usr/bin/sshpass -p ${1} /usr/bin/scp -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} -r ${2}/* ${3}@${4}:${5}/ 2> /dev/null
|
|
elif [ "${toolSelected}" == "rsync-pem" ] ; then
|
|
echo ""
|
|
echo "+ Syncing folder ${2} to ${5} (${4})"
|
|
/usr/bin/rsync --progress -azL --rsh="/usr/bin/ssh -i ${dirConfig}/connect.pem -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} -l ${3}" ${2}/ ${4}:${5}/ 2> /dev/null
|
|
/usr/bin/ssh -i ${dirConfig}/connect.pem -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} rm -rf ${5}/.${6}.* &> /dev/null
|
|
elif [ "${toolSelected}" == "scp-pem" ] ; then
|
|
echo ""
|
|
echo "+ Sending files from ${2} to ${5} (${4})"
|
|
scp -i ${dirConfig}/connect.pem -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} -r ${2}/* ${3}@${4}:${5}/ 2> /dev/null
|
|
else
|
|
echo ""
|
|
echo "+ Sending files from ${2} to ${5} (${4})"
|
|
/usr/bin/sshpass -p ${1} /usr/bin/scp -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} -r ${2}/* ${3}@${4}:${5}/ 2> /dev/null
|
|
fi
|
|
# Run command for upload on Unix systems
|
|
else
|
|
if [ "${toolSelected}" == "rsync" ] ; then
|
|
echo ""
|
|
echo "+ Syncing folder ${2} to ${5} (${4})"
|
|
rsync --progress -azL --rsh="sshpass -p ${1} ssh -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} -l ${3}" ${2}/ ${4}:${5}/ 2> /dev/null
|
|
sshpass -p ${1} ssh -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} rm -rf ${5}/.${6}.* &> /dev/null
|
|
elif [ "${toolSelected}" == "rsync-e" ] ; then
|
|
keyscan_host=$(cat ~/.ssh/known_hosts | grep ${4})
|
|
if [ -z "${keyscan_host}" ] ; then
|
|
ssh-keyscan -p ${portSelected} ${4} >> ~/.ssh/known_hosts
|
|
fi
|
|
# Sync files using rsync with expect
|
|
expect <<EOF
|
|
spawn rsync --progress -rlDzL --port=${portSelected} ${2}/ ${3}@${4}:${5}/
|
|
set timeout -1
|
|
expect "*?assword:*"
|
|
send "${1}\n"
|
|
expect eof
|
|
EOF
|
|
OUTPUT=$?
|
|
elif [ "${toolSelected}" == "scp-e" ] ; then
|
|
# Sync files using scp with expect
|
|
expect <<EOF
|
|
spawn scp -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} -r ${2}/ ${3}@${4}:${5}/
|
|
set timeout -1
|
|
expect "*?assword:*"
|
|
send "${1}\n"
|
|
expect eof
|
|
EOF
|
|
OUTPUT=$?
|
|
elif [ "${toolSelected}" == "rsync-rsa" ] ; then
|
|
echo ""
|
|
echo "+ Syncing folder ${2} to ${5} (${4})"
|
|
rsync --progress -azL --rsh="sshpass -p ${1} ssh -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} -l ${3}" ${2}/ ${4}:${5}/ 2> /dev/null
|
|
sshpass -p ${1} ssh -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} rm -rf ${5}/.${6}.* &> /dev/null
|
|
elif [ "${toolSelected}" == "scp-rsa" ] ; then
|
|
echo ""
|
|
echo "+ Sending files from ${2} to ${5} (${4})"
|
|
sshpass -p ${1} scp -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} -r ${2}/* ${3}@${4}:${5}/ 2> /dev/null
|
|
elif [ "${toolSelected}" == "rsync-pem" ] ; then
|
|
echo ""
|
|
echo "+ Syncing folder ${2} to ${5} (${4})"
|
|
rsync --progress -azL --rsh="ssh -i ${dirConfig}/connect.pem -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} -l ${3}" ${2}/ ${4}:${5}/ 2> /dev/null
|
|
ssh -i ${dirConfig}/connect.pem -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} rm -rf ${5}/.${6}.* &> /dev/null
|
|
elif [ "${toolSelected}" == "scp-pem" ] ; then
|
|
echo ""
|
|
echo "+ Sending files from ${2} to ${5} (${4})"
|
|
scp -i ${dirConfig}/connect.pem -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} -r ${2}/* ${3}@${4}:${5}/ 2> /dev/null
|
|
else
|
|
echo ""
|
|
echo "+ Sending files from ${2} to ${5} (${4})"
|
|
sshpass -p ${1} scp -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} -r ${2}/* ${3}@${4}:${5}/ 2> /dev/null
|
|
fi
|
|
fi
|
|
}
|
|
|
|
# Function for recursive checksum.
|
|
# # Syntax: recursive_checksum <password> <file path> <user> <server> <server path>
|
|
function recursive_checksum() {
|
|
toolSelected=$(cat ${dirConfig}/ks-upload-tool)
|
|
portSelected=$(cat ${dirConfig}/ks-upload-port)
|
|
echo "# Generating checksum for files on ${dirLocal} directory"
|
|
cd ${2}
|
|
find . -type f -exec md5sum {} > ${dirConfig}/ks-tools_checksum.md5 \;
|
|
if [ "${cygwin}" == "yes" ] ; then
|
|
if [ "${toolSelected}" == "rsync" ] ; then
|
|
echo "# Copying checksum file to ${4}"
|
|
/usr/bin/sshpass -p ${1} /usr/bin/scp -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} ${dirConfig}/ks-tools_checksum.md5 ${3}@${4}:${5}/ &> /dev/null
|
|
echo "# Running checksum test"
|
|
/usr/bin/sshpass -p ${1} /usr/bin/ssh -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && md5sum -c ks-tools_checksum.md5"
|
|
/usr/bin/sshpass -p ${1} /usr/bin/ssh -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && rm -rf ks-tools_checksum.md5"
|
|
elif [ "${toolSelected}" == "scp" ] ; then
|
|
echo "# Copying checksum file to ${4}"
|
|
/usr/bin/sshpass -p ${1} /usr/bin/scp -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} ${dirConfig}/ks-tools_checksum.md5 ${3}@${4}:${5}/ &> /dev/null
|
|
echo "# Running checksum test"
|
|
/usr/bin/sshpass -p ${1} /usr/bin/ssh -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && md5sum -c ks-tools_checksum.md5"
|
|
/usr/bin/sshpass -p ${1} /usr/bin/ssh -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && rm -rf ks-tools_checksum.md5"
|
|
elif [ "${toolSelected}" == "rsync-rsa" ] ; then
|
|
echo "# Copying checksum file to ${4}"
|
|
/usr/bin/sshpass -p ${1} /usr/bin/scp -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} ${dirConfig}/ks-tools_checksum.md5 ${3}@${4}:${5}/ &> /dev/null
|
|
echo "# Running checksum test"
|
|
/usr/bin/sshpass -p ${1} /usr/bin/ssh -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && md5sum -c ks-tools_checksum.md5"
|
|
/usr/bin/sshpass -p ${1} /usr/bin/ssh -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && rm -rf ks-tools_checksum.md5"
|
|
elif [ "${toolSelected}" == "scp-rsa" ] ; then
|
|
echo "# Copying checksum file to ${4}"
|
|
/usr/bin/sshpass -p ${1} /usr/bin/scp -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} ${dirConfig}/ks-tools_checksum.md5 ${3}@${4}:${5}/ &> /dev/null
|
|
echo "# Running checksum test"
|
|
/usr/bin/sshpass -p ${1} /usr/bin/ssh -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && md5sum -c ks-tools_checksum.md5"
|
|
/usr/bin/sshpass -p ${1} /usr/bin/ssh -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && rm -rf ks-tools_checksum.md5"
|
|
elif [ "${toolSelected}" == "rsync-pem" ] ; then
|
|
echo "# Copying checksum file to ${4}"
|
|
/usr/bin/scp -i ${dirConfig}/connect.pem -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} ${dirConfig}/ks-tools_checksum.md5 ${3}@${4}:${5}/ &> /dev/null
|
|
echo "# Running checksum test"
|
|
/usr/bin/ssh -i ${dirConfig}/connect.pem -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && md5sum -c ks-tools_checksum.md5"
|
|
/usr/bin/ssh -i ${dirConfig}/connect.pem -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && rm -rf ks-tools_checksum.md5"
|
|
elif [ "${toolSelected}" == "scp-pem" ] ; then
|
|
echo "# Copying checksum file to ${4}"
|
|
/usr/bin/scp -i ${dirConfig}/connect.pem -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} ${dirConfig}/ks-tools_checksum.md5 ${3}@${4}:${5}/ &> /dev/null
|
|
echo "# Running checksum test"
|
|
/usr/bin/ssh -i ${dirConfig}/connect.pem -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && md5sum -c ks-tools_checksum.md5"
|
|
/usr/bin/ssh -i ${dirConfig}/connect.pem -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && rm -rf ks-tools_checksum.md5"
|
|
elif [ "${toolSelected}" == "rsync-e" ] ; then
|
|
echo "# Cancelled checksum por rsync-e"
|
|
elif [ "${toolSelected}" == "scp-pem" ] ; then
|
|
echo "# Cancelled checksum for scp-e"
|
|
fi
|
|
else
|
|
if [ "${toolSelected}" == "rsync" ] ; then
|
|
echo "# Copying checksum file to ${4}"
|
|
sshpass -p ${1} scp -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} ${dirConfig}/ks-tools_checksum.md5 ${3}@${4}:${5}/ &> /dev/null
|
|
echo "# Running checksum test"
|
|
sshpass -p ${1} ssh -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && md5sum -c ks-tools_checksum.md5"
|
|
sshpass -p ${1} ssh -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && rm -rf ks-tools_checksum.md5"
|
|
elif [ "${toolSelected}" == "scp" ] ; then
|
|
echo "# Copying checksum file to ${4}"
|
|
sshpass -p ${1} scp -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} ${dirConfig}/ks-tools_checksum.md5 ${3}@${4}:${5}/ &> /dev/null
|
|
echo "# Running checksum test"
|
|
sshpass -p ${1} ssh -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && md5sum -c ks-tools_checksum.md5"
|
|
sshpass -p ${1} ssh -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && rm -rf ks-tools_checksum.md5"
|
|
elif [ "${toolSelected}" == "rsync-rsa" ] ; then
|
|
echo "# Copying checksum file to ${4}"
|
|
sshpass -p ${1} scp -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} ${dirConfig}/ks-tools_checksum.md5 ${3}@${4}:${5}/ &> /dev/null
|
|
echo "# Running checksum test"
|
|
sshpass -p ${1} ssh -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && md5sum -c ks-tools_checksum.md5"
|
|
sshpass -p ${1} ssh -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && rm -rf ks-tools_checksum.md5"
|
|
elif [ "${toolSelected}" == "scp-rsa" ] ; then
|
|
echo "# Copying checksum file to ${4}"
|
|
sshpass -p ${1} scp -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} ${dirConfig}/ks-tools_checksum.md5 ${3}@${4}:${5}/ &> /dev/null
|
|
echo "# Running checksum test"
|
|
sshpass -p ${1} ssh -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && md5sum -c ks-tools_checksum.md5"
|
|
sshpass -p ${1} ssh -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && rm -rf ks-tools_checksum.md5"
|
|
elif [ "${toolSelected}" == "rsync-pem" ] ; then
|
|
echo "# Copying checksum file to ${4}"
|
|
scp -i ${dirConfig}/connect.pem -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} ${dirConfig}/ks-tools_checksum.md5 ${3}@${4}:${5}/ &> /dev/null
|
|
echo "# Running checksum test"
|
|
ssh -i ${dirConfig}/connect.pem -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && md5sum -c ks-tools_checksum.md5"
|
|
ssh -i ${dirConfig}/connect.pem -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && rm -rf ks-tools_checksum.md5"
|
|
elif [ "${toolSelected}" == "scp-pem" ] ; then
|
|
echo "# Copying checksum file to ${4}"
|
|
scp -i ${dirConfig}/connect.pem -o StrictHostKeyChecking=no -o CheckHostIP=no -P ${portSelected} ${dirConfig}/ks-tools_checksum.md5 ${3}@${4}:${5}/ &> /dev/null
|
|
echo "# Running checksum test"
|
|
ssh -i ${dirConfig}/connect.pem -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && md5sum -c ks-tools_checksum.md5"
|
|
ssh -i ${dirConfig}/connect.pem -o StrictHostKeyChecking=no -o CheckHostIP=no -p ${portSelected} ${3}@${4} "cd ${5} && rm -rf ks-tools_checksum.md5"
|
|
elif [ "${toolSelected}" == "rsync-e" ] ; then
|
|
echo "# Cancelled checksum por rsync-e"
|
|
elif [ "${toolSelected}" == "scp-pem" ] ; then
|
|
echo "# Cancelled checksum for scp-e"
|
|
fi
|
|
fi
|
|
}
|
|
|
|
# Generate random codes
|
|
function generate_codes() {
|
|
chars="abcdefghijklmnopqrstywxz1234567890ABCDEFHIJKLMNOPQRSTYWXZ@/"
|
|
long_code="${1}"
|
|
for i in {1} ; do
|
|
echo -n "${chars:RANDOM%${#chars}:1}"
|
|
done
|
|
}
|
|
|
|
# Function to encrypt pass on config file
|
|
# Syntax: encrypt_kstools <password>
|
|
function encrypt_kstools() {
|
|
raw_pass="${1}"
|
|
characters_pass_raw=$(echo ${raw_pass} | wc -m)
|
|
characters_pass=$(expr ${characters_pass_raw} - 1)
|
|
total_characters=0
|
|
mkdir -p ${HOME}/.ks-tools/
|
|
rm -rf ${HOME}/.ks-tools/.seq-codes
|
|
while [ ${total_characters} -le ${characters_pass} ] ; do
|
|
num_gen=$(echo -n ${RANDOM} | cut -c1)
|
|
echo -n ${num_gen} >> ${HOME}/.ks-tools/.seq-codes
|
|
total_characters=$(expr ${total_characters} + 1)
|
|
done
|
|
characters_seq_raw=$(cat ${HOME}/.ks-tools/.seq-codes 2> /dev/null | wc -m)
|
|
characters_seq=$(expr ${characters_seq_raw} - 1)
|
|
total_characters=1
|
|
encrypted_pass=""
|
|
while [ ${total_characters} -le ${characters_pass} ] ; do
|
|
num_seq_read=$(cat ${HOME}/.ks-tools/.seq-codes 2> /dev/null | cut -c${total_characters})
|
|
character=$(echo ${raw_pass} | cut -c${total_characters})
|
|
repeat_seq=0
|
|
while [ ${repeat_seq} -lt ${num_seq_read} ] ; do
|
|
code_gen=$(generate_codes)
|
|
encrypted_pass="${encrypted_pass}${code_gen}"
|
|
repeat_seq=$(expr ${repeat_seq} + 1)
|
|
done
|
|
encrypted_pass="${encrypted_pass}${character}"
|
|
total_characters=$(expr ${total_characters} + 1)
|
|
done
|
|
code_gen=$(generate_codes)
|
|
encrypted_pass="${encrypted_pass}${code_gen}"
|
|
echo ${encrypted_pass}
|
|
}
|
|
|
|
# Function to decrypt pass on config file
|
|
# Syntax: decrypt_kstools <password>
|
|
function decrypt_kstools() {
|
|
raw_pass_encrypted="${1}"
|
|
total_characters=1
|
|
codes_seq=$(cat ${HOME}/.ks-tools/.seq-codes 2> /dev/null)
|
|
num_codes_seq=$(cat ${HOME}/.ks-tools/.seq-codes 2> /dev/null | wc -m)
|
|
pass_decrypt=""
|
|
total_characters=1
|
|
pos_codes_pass=0
|
|
while [ ${total_characters} -lt ${num_codes_seq} ] ; do
|
|
pos_codes=$(echo ${codes_seq} | cut -c${total_characters})
|
|
pos_codes_pass=$(expr ${pos_codes_pass} + ${pos_codes} + 1)
|
|
pos_pass=$(expr ${raw_pass_encrypted} | cut -c${pos_codes_pass})
|
|
pass_decrypt="${pass_decrypt}${pos_pass}"
|
|
total_characters=$(expr ${total_characters} + 1)
|
|
done
|
|
echo ${pass_decrypt}
|
|
}
|
|
|
|
# Function to generate certificate on remote machine
|
|
function gen_pem_ssh() {
|
|
userhost="${USER}"
|
|
name_host="${HOSTNAME}"
|
|
echo "" && echo "* Generating .pem file to connect to this machine (${userhost})."
|
|
echo -e "${userhost}\n\n\n" | ssh-keygen
|
|
mv ${userhost} ${userhost}-${name_host}.pem
|
|
mkdir -p ~/.ssh
|
|
chmod 700 ~/.ssh
|
|
touch ~/.ssh/authorized_keys
|
|
chmod 600 ~/.ssh/authorized_keys
|
|
echo "* Adding entry in the file ~/.ssh/authorized_keys"
|
|
cat ${userhost}.pub >> ~/.ssh/authorized_keys
|
|
rm -rf ${userhost}.pub
|
|
echo ""
|
|
echo "+ Copy the ${userhost}-${name_host}.pem file to your computer to connect to this machine."
|
|
if [ -f /usr/bin/puttygen ] ; then
|
|
puttygen ${userhost}-${name_host}.pem -o ${userhost}-${name_host}.ppk -O private
|
|
echo "+ Copy the ${userhost}-${name_host}.ppk file to your computer to connect to this machine."
|
|
fi
|
|
echo "+ Connect: ssh -i ${userhost}-${name_host}.pem ${userhost}@${name_host}"
|
|
echo ""
|
|
}
|
|
|
|
# Function to show config
|
|
function showConfig() {
|
|
configAvailable=0
|
|
echo ""
|
|
echo "* ks-upr (ks-tools) v${VERSION} (${M_DATE})"
|
|
echo ""
|
|
if [ -f ${dirConfig}/ks-upload-user ] ; then
|
|
showUser=$(cat ${dirConfig}/ks-upload-user)
|
|
echo "- Server User: ${showUser}"
|
|
configAvailable=1
|
|
fi
|
|
if [ -f ${dirConfig}/ks-upload-pass ] ; then
|
|
readPasswordEncrypted=$(cat ${dirConfig}/ks-upload-pass)
|
|
readPasswordDecrypted=$(decrypt_kstools "${readPasswordEncrypted}")
|
|
readPasswordChars=$(echo ${readPasswordDecrypted} | wc -m)
|
|
totalchars=0
|
|
showPassword=""
|
|
while [ ${totalchars} -lt ${readPasswordChars} ] ; do
|
|
showPassword="${showPassword}*"
|
|
totalchars=$(expr ${totalchars} + 1)
|
|
done
|
|
echo "- Server Password/PEM: ${showPassword}"
|
|
configAvailable=1
|
|
fi
|
|
if [ -f ${dirConfig}/ks-upload-server ] ; then
|
|
showServer=$(cat ${dirConfig}/ks-upload-server)
|
|
echo "- URL (or IP) Server: ${showServer}"
|
|
configAvailable=1
|
|
fi
|
|
if [ -f ${dirConfig}/ks-upload-dirserver ] ; then
|
|
showDirServer=$(cat ${dirConfig}/ks-upload-dirserver)
|
|
echo "- Destination Path (Server): ${showDirServer}"
|
|
configAvailable=1
|
|
fi
|
|
if [ -f ${dirConfig}/ks-upload-dirlocal ] ; then
|
|
showDirLocal=$(cat ${dirConfig}/ks-upload-dirlocal)
|
|
echo "- Scan Path (Local): ${showDirLocal}"
|
|
configAvailable=1
|
|
fi
|
|
if [ -f ${dirConfig}/ks-upload-tool ] ; then
|
|
showUpvTool=$(cat ${dirConfig}/ks-upload-tool)
|
|
echo "- Upload Tool: ${showUpvTool}"
|
|
configAvailable=1
|
|
fi
|
|
if [ -f ${dirConfig}/ks-upload-port ] ; then
|
|
showUpvPort=$(cat ${dirConfig}/ks-upload-port)
|
|
echo "- Port Connection: ${showUpvPort}"
|
|
configAvailable=1
|
|
fi
|
|
if [ ${configAvailable} -eq 0 ] ; then
|
|
echo "* The configuration file does not exist!"
|
|
fi
|
|
echo ""
|
|
exit
|
|
}
|
|
|
|
# Function to edit the configuration file
|
|
function editConfig() {
|
|
editConfig=0
|
|
while [ ${editConfig} -eq 0 ] ; do
|
|
clear
|
|
editUser=$(cat ${dirConfig}/ks-upload-user 2> /dev/null)
|
|
readPasswordEncrypted=$(cat ${dirConfig}/ks-upload-pass)
|
|
readPasswordDecrypted=$(decrypt_kstools "${readPasswordEncrypted}")
|
|
readPasswordChars=$(echo ${readPasswordDecrypted} | wc -m)
|
|
totalchars=0
|
|
editPassword=""
|
|
while [ ${totalchars} -lt ${readPasswordChars} ] ; do
|
|
editPassword="${editPassword}*"
|
|
totalchars=$(expr ${totalchars} + 1)
|
|
done
|
|
editServer=$(cat ${dirConfig}/ks-upload-server 2> /dev/null)
|
|
editDirServer=$(cat ${dirConfig}/ks-upload-dirserver 2> /dev/null)
|
|
editDirLocal=$(cat ${dirConfig}/ks-upload-dirlocal 2> /dev/null)
|
|
editUpvTool=$(cat ${dirConfig}/ks-upload-tool 2> /dev/null)
|
|
editUpvPort=$(cat ${dirConfig}/ks-upload-port 2> /dev/null)
|
|
echo ""
|
|
echo "* ks-upr (ks-tools) v${VERSION} (${M_DATE})"
|
|
echo ""
|
|
echo "+ Config: ${dirConfig}/ks-upload-*"
|
|
echo ""
|
|
echo " 1 - Edit User (${editUser})"
|
|
echo " 2 - Edit Password/PEM (${editPassword})"
|
|
echo " 3 - Edit Server (${editServer})"
|
|
echo " 4 - Edit Dest. Path (${editDirServer})"
|
|
echo " 5 - Edit Local Path (${editDirLocal})"
|
|
echo " 6 - Edit Upload Tool (${editUpvTool})"
|
|
echo " 7 - Edit Port Connection (${editUpvPort})"
|
|
echo ""
|
|
echo " 8 - Exit"
|
|
echo ""
|
|
echo -n "* Choose an option: " ; read EDIT
|
|
echo ""
|
|
if [ "${EDIT}" == "1" ] ; then
|
|
echo -n "* Enter the server user: " ; read USER
|
|
user=${USER}
|
|
echo ${user} > ${dirConfig}/ks-upload-user
|
|
elif [ "${EDIT}" == "2" ] ; then
|
|
echo -n "* Enter the server password (type 'pem' for use pem certificate): " ; read PASS
|
|
if [ "${PASS}" == "pem" ] ; then
|
|
echo -n "+ Type the absolute path to the .pem file: " ; read pem_path
|
|
if [ -f "${pem_path}" ] ; then
|
|
raw_password_enter=${PASS}
|
|
encrypt_password_enter=$(encrypt_kstools "${raw_password_enter}")
|
|
echo ${encrypt_password_enter} > ${dirConfig}/ks-upload-pass
|
|
cp -rf "${pem_path}" ${dirConfig}/connect.pem
|
|
chmod 600 ${dirConfig}/connect.pem
|
|
if [ "${editUpvTool}" == "rsync" ] ; then
|
|
echo "rsync-pem" > ${dirConfig}/ks-upload-tool
|
|
elif [ "${editUpvTool}" == "rsync-rsa" ] ; then
|
|
echo "rsync-pem" > ${dirConfig}/ks-upload-tool
|
|
elif [ "${editUpvTool}" == "scp" ] ; then
|
|
echo "scp-pem" > ${dirConfig}/ks-upload-tool
|
|
elif [ "${editUpvTool}" == "scp-rsa" ] ; then
|
|
echo "scp-pem" > ${dirConfig}/ks-upload-tool
|
|
fi
|
|
echo "* PEM file copied to ${dirConfig}/connect.pem"
|
|
else
|
|
echo "+ File ${pem_path} does not exist"
|
|
echo ""
|
|
echo "+ Press ENTER to return menu " ; read continue
|
|
fi
|
|
else
|
|
raw_password_enter=${PASS}
|
|
encrypt_password_enter=$(encrypt_kstools "${raw_password_enter}")
|
|
echo ${encrypt_password_enter} > ${dirConfig}/ks-upload-pass
|
|
if [ "${editUpvTool}" == "rsync-pem" ] ; then
|
|
echo "rsync" > ${dirConfig}/ks-upload-tool
|
|
elif [ "${editUpvTool}" == "scp-pem" ] ; then
|
|
echo "scp" > ${dirConfig}/ks-upload-tool
|
|
fi
|
|
fi
|
|
elif [ "${EDIT}" == "3" ] ; then
|
|
echo -n "* Enter the server URL: " ; read SERVER
|
|
server=${SERVER}
|
|
echo ${server} > ${dirConfig}/ks-upload-server
|
|
elif [ "${EDIT}" == "4" ] ; then
|
|
echo -n "* Enter the path on the server: " ; read DIR_SERVER
|
|
dirServer=${DIR_SERVER}
|
|
echo ${dirServer} > ${dirConfig}/ks-upload-dirserver
|
|
elif [ "${EDIT}" == "5" ] ; then
|
|
echo -n "* Enter the local path to scan: " ; read DIR
|
|
dirLocal=${DIR}
|
|
echo ${dirLocal} > ${dirConfig}/ks-upload-dirlocal
|
|
elif [ "${EDIT}" == "6" ] ; then
|
|
echo ""
|
|
echo "* Available options:"
|
|
echo ""
|
|
echo "- scp"
|
|
echo "- rsync"
|
|
echo "- scp-rsa (legacy RSA algorithm)"
|
|
echo "- rsync-rsa (legacy RSA algorithm)"
|
|
echo "- scp-e"
|
|
echo "- rsync-e"
|
|
echo "- scp-pem"
|
|
echo "- rsync-pem"
|
|
echo ""
|
|
echo "* Note: 'scp-e' and 'rsync-e' use 'expect' instead of 'sshpass'."
|
|
echo "* Note: 'scp-pem' and 'rsync-pem' use ~/.ks-tools/connect.pem."
|
|
echo ""
|
|
echo -n "* [Default: scp] Type upload tool (Example: rsync-e): " ; read TOOL
|
|
if [ -z "${TOOL}" ] ; then
|
|
upvTool="scp"
|
|
elif [ "${TOOL}" == "rsync" ] ; then
|
|
upvTool="rsync"
|
|
elif [ "${TOOL}" == "rsync-rsa" ] ; then
|
|
upvTool="rsync-rsa"
|
|
elif [ "${TOOL}" == "scp-rsa" ] ; then
|
|
upvTool="scp-rsa"
|
|
elif [ "${TOOL}" == "scp-e" ] ; then
|
|
upvTool="scp-e"
|
|
elif [ "${TOOL}" == "rsync-e" ] ; then
|
|
upvTool="rsync-e"
|
|
elif [ "${TOOL}" == "scp-pem" ] ; then
|
|
upvTool="scp-pem"
|
|
elif [ "${TOOL}" == "rsync-pem" ] ; then
|
|
upvTool="rsync-pem"
|
|
else
|
|
upvTool="scp"
|
|
fi
|
|
echo ${upvTool} > ${dirConfig}/ks-upload-tool
|
|
elif [ "${EDIT}" == "7" ] ; then
|
|
echo -n "* [Default: 22] Type port connection: " ; read PORT
|
|
if [ -z "${PORT}" ] ; then
|
|
upvPort="22"
|
|
else
|
|
upvPort="${PORT}"
|
|
fi
|
|
echo ${upvPort} > ${dirConfig}/ks-upload-port
|
|
elif [ "${EDIT}" == "8" ] ; then
|
|
editConfig=1
|
|
else
|
|
echo "+ Invalid option!"
|
|
echo -n "- Press ENTER to continue " ; read CONTINUE
|
|
fi
|
|
done
|
|
exit
|
|
}
|
|
|
|
# Function to create initial config file.
|
|
function createConfig() {
|
|
# Start script
|
|
clear
|
|
echo ""
|
|
echo "* ks-upr (ks-tools) v${VERSION} (${M_DATE})"
|
|
echo ""
|
|
checkDependencies
|
|
# Ask the user and if it exists, read it from the config.
|
|
if [ -f ${dirConfig}/ks-upload-user ] ; then
|
|
echo "+ The USER configuration already exists (Use: 'ks-upr -e' for edit)"
|
|
else
|
|
echo -n "* Enter the server user: " ; read USER
|
|
user=${USER}
|
|
echo ${user} > ${dirConfig}/ks-upload-user
|
|
fi
|
|
# Ask the password and if it exists, read it from the config.
|
|
if [ -f ${dirConfig}/ks-upload-pass ] ; then
|
|
echo "+ The PASSWORD/PEM configuration already exists (Use: 'ks-upr -e' for edit)"
|
|
else
|
|
echo -n "* Enter the server password (type 'pem' for use pem certificate): " ; read PASS
|
|
if [ "${PASS}" == "pem" ] ; then
|
|
echo -n "+ Type the absolute path to the .pem file: " ; read pem_path
|
|
if [ -f "${pem_path}" ] ; then
|
|
raw_password_enter=${PASS}
|
|
encrypt_password_enter=$(encrypt_kstools "${raw_password_enter}")
|
|
echo ${encrypt_password_enter} > ${dirConfig}/ks-upload-pass
|
|
cp -rf "${pem_path}" ${dirConfig}/connect.pem
|
|
chmod 600 ${dirConfig}/connect.pem
|
|
echo "scp-pem" > ${dirConfig}/ks-upload-tool
|
|
echo "* PEM file copied to ${dirConfig}/connect.pem"
|
|
else
|
|
echo "+ File ${pem_path} does not exist"
|
|
echo "+ The configuration is cancelled"
|
|
exit
|
|
fi
|
|
else
|
|
raw_password_enter=${PASS}
|
|
encrypt_password_enter=$(encrypt_kstools "${raw_password_enter}")
|
|
echo ${encrypt_password_enter} > ${dirConfig}/ks-upload-pass
|
|
fi
|
|
fi
|
|
# Ask the server URL and if it exists, read it from the config.
|
|
if [ -f ${dirConfig}/ks-upload-server ] ; then
|
|
echo "+ The SERVER configuration already exists (Use: 'ks-upr -e' for edit)"
|
|
else
|
|
echo -n "* Enter the server URL (or IP): " ; read SERVER
|
|
server=${SERVER}
|
|
echo ${server} > ${dirConfig}/ks-upload-server
|
|
fi
|
|
# Ask the path on the server and if it exists, read it from the config.
|
|
if [ -f ${dirConfig}/ks-upload-dirserver ] ; then
|
|
echo "+ The SERVER PATH configuration already exists (Use: 'ks-upr -e' for edit)"
|
|
else
|
|
echo -n "* Enter the path on the server: " ; read DIR_SERVER
|
|
dirServer=${DIR_SERVER}
|
|
echo ${dirServer} > ${dirConfig}/ks-upload-dirserver
|
|
fi
|
|
# Ask the local path and if it exists, read it from the config.
|
|
if [ -f ${dirConfig}/ks-upload-dirlocal ] ; then
|
|
echo "+ The LOCAL PATH configuration already exists (Use: 'ks-upr -e' for edit)"
|
|
else
|
|
echo -n "* Enter the local path to scan: " ; read DIR
|
|
dirLocal=${DIR}
|
|
echo ${dirLocal} > ${dirConfig}/ks-upload-dirlocal
|
|
fi
|
|
# Configure upload tool by default
|
|
if [ -f ${dirConfig}/ks-upload-tool ] ; then
|
|
echo "+ The UPLOAD TOOL configuration already exists (Use: 'ks-upr -e' for edit)"
|
|
else
|
|
if [ "${PASS}" == "pem" ] ; then
|
|
echo "* Setting 'scp-pem' as the default tool"
|
|
echo "scp-pem" > ${dirConfig}/ks-upload-tool
|
|
else
|
|
echo "* Setting 'scp' as the default tool"
|
|
echo "scp" > ${dirConfig}/ks-upload-tool
|
|
fi
|
|
fi
|
|
# Configure port by default
|
|
if [ -f ${dirConfig}/ks-upload-port ] ; then
|
|
echo "+ The PORT configuration already exists (Use: 'ks-upr -e' for edit)"
|
|
else
|
|
echo "* Setting port 22 as the default port"
|
|
echo "22" > ${dirConfig}/ks-upload-port
|
|
fi
|
|
echo ""
|
|
}
|
|
|
|
# Function to show version
|
|
function showVersion() {
|
|
echo ""
|
|
echo "* ks-upr (ks-tools) v${VERSION} (${M_DATE})"
|
|
echo ""
|
|
exit
|
|
}
|
|
|
|
# Function to show help
|
|
function showHelp() {
|
|
echo ""
|
|
echo "* ks-upr (ks-tools) v${VERSION} (${M_DATE})"
|
|
echo ""
|
|
echo "- Upload recursively file(s) to server with scp or rsync"
|
|
echo ""
|
|
echo "+ Config: ${dirConfig}/ks-upload-*"
|
|
echo ""
|
|
echo "+ Syntax:"
|
|
echo ""
|
|
echo " $ ks-upr -i - Start upload"
|
|
echo " $ ks-upr -g - Create configuration"
|
|
echo " $ ks-upr -r - Remove configuration"
|
|
echo " $ ks-upr -c - Show configuration"
|
|
echo " $ ks-upr -e - Edit configuration"
|
|
echo " $ ks-upr -p - Generate certificate"
|
|
echo " $ ks-upr -v - Show version"
|
|
echo " $ ks-upr -h - Show help"
|
|
echo ""
|
|
exit
|
|
}
|
|
|
|
# Function to check if all the necessary tools
|
|
# for the execution are installed.
|
|
function checkDependencies() {
|
|
path_check="/usr/bin /bin /usr/local/bin ${HOME}/.local/bin /home/linuxbrew/.linuxbrew/bin"
|
|
dependencies="sshpass expect rsync md5sum scp ssh find grep sed cut head tail cat ssh-keyscan ssh-keygen"
|
|
dependencies_found=""
|
|
dependencies_not_found=""
|
|
for checkPath in ${path_check} ; do
|
|
for checkDependencies in ${dependencies} ; do
|
|
if [ -f ${checkPath}/${checkDependencies} ] ; then
|
|
dependencies_found="${dependencies_found} ${checkDependencies}"
|
|
fi
|
|
done
|
|
done
|
|
for notFound in ${dependencies} ; do
|
|
check_found_one=$(echo ${dependencies_found} | grep " ${notFound}")
|
|
check_found_two=$(echo ${dependencies_found} | grep "${notFound} ")
|
|
if_not_found="${check_found_one}${check_found_two}"
|
|
if [ -z "${if_not_found}" ] ; then
|
|
dependencies_not_found="${dependencies_not_found} ${notFound}"
|
|
fi
|
|
done
|
|
echo "* Checking necessary tools... "
|
|
# Show if all tools are installed
|
|
if [ -z "${dependencies_not_found}" ] ; then
|
|
echo "* The necessary tools are installed"
|
|
echo ""
|
|
else
|
|
echo "* Some required tools are not installed:${dependencies_not_found}"
|
|
echo "* The process has been stopped"
|
|
echo ""
|
|
exit
|
|
fi
|
|
}
|
|
|
|
# Function to migrate old configuration.
|
|
if [ -f ${dirConfig}/USER ] ; then
|
|
cat ${dirConfig}/USER > ${dirConfig}/ks-upload-user
|
|
echo "* Migrating ${dirConfig}/USER > ${dirConfig}/ks-upload-user"
|
|
rm -rf ${dirConfig}/USER
|
|
sleep 1
|
|
fi
|
|
if [ -f ${dirConfig}/PASS ] ; then
|
|
echo "* Migrating ${dirConfig}/PASS > ${dirConfig}/ks-upload-pass"
|
|
current_pass=$(cat ${dirConfig}/PASS)
|
|
encrypt_kstools "${current_pass}" > ${dirConfig}/ks-upload-pass
|
|
rm -rf ${dirConfig}/PASS
|
|
sleep 1
|
|
fi
|
|
if [ -f ${dirConfig}/SERVER ] ; then
|
|
cat ${dirConfig}/SERVER > ${dirConfig}/ks-upload-server
|
|
echo "* Migrating ${dirConfig}/SERVER > ${dirConfig}/ks-upload-server"
|
|
rm -rf ${dirConfig}/SERVER
|
|
sleep 1
|
|
fi
|
|
if [ -f ${dirConfig}/DIR_SERVER ] ; then
|
|
cat ${dirConfig}/DIR_SERVER > ${dirConfig}/ks-upload-dirserver
|
|
echo "* Migrating ${dirConfig}/DIR_SERVER > ${dirConfig}/ks-upload-dirserver"
|
|
rm -rf ${dirConfig}/DIR_SERVER
|
|
sleep 1
|
|
fi
|
|
if [ -f ${dirConfig}/DIR ] ; then
|
|
cat ${dirConfig}/DIR > ${dirConfig}/ks-upload-dirlocal
|
|
echo "* Migrating ${dirConfig}/DIR > ${dirConfig}/ks-upload-dirlocal"
|
|
rm -rf ${dirConfig}/DIR
|
|
sleep 1
|
|
fi
|
|
|
|
# Check if the configuration directory exists.
|
|
if [ -d ${dirConfig} ] ; then
|
|
echo ${dirConfig} > /dev/null
|
|
else
|
|
mkdir -p ${dirConfig}
|
|
fi
|
|
|
|
# Delete the existing configuration.
|
|
if [ "${1}" == "-r" ] ; then
|
|
echo ""
|
|
echo "* ks-upr (ks-tools) v${VERSION} (${M_DATE})"
|
|
echo ""
|
|
echo -n "* [Default: n] Are you sure you want to delete the configuration? (y/n): " ; read confirm
|
|
if [ "${confirm}" == "y" ] ; then
|
|
echo ""
|
|
echo "+ Removing USER file configuration..."
|
|
rm -rf ${dirConfig}/ks-upload-user
|
|
echo "+ Removing PASSWORD/PEM file configuration..."
|
|
rm -rf ${dirConfig}/ks-upload-pass
|
|
rm -rf ${dirConfig}/connect.pem
|
|
echo "+ Removing SERVER file configuration..."
|
|
rm -rf ${dirConfig}/ks-upload-server
|
|
echo "+ Removing SERVER PATH file configuration..."
|
|
rm -rf ${dirConfig}/ks-upload-dirserver
|
|
echo "+ Removing LOCAL PATH file configuration..."
|
|
rm -rf ${dirConfig}/ks-upload-dirlocal
|
|
echo "* Removed all configuration"
|
|
rm -rf ${dirConfig}/ks-upload-tool
|
|
rm -rf ${dirConfig}/ks-upload-port
|
|
echo ""
|
|
exit
|
|
else
|
|
echo ""
|
|
echo "+ Remove configuration canceled"
|
|
echo ""
|
|
exit
|
|
fi
|
|
fi
|
|
# Show configuration file
|
|
if [ "${1}" == "-c" ] ; then
|
|
showConfig
|
|
# Edit configuration file
|
|
elif [ "${1}" == "-e" ] ; then
|
|
editConfig
|
|
# Show the version
|
|
elif [ "${1}" == "-v" ] ; then
|
|
showVersion
|
|
# Generate certificate
|
|
elif [ "${1}" == "-p" ] ; then
|
|
gen_pem_ssh
|
|
# Show the help
|
|
elif [ "${1}" == "-h" ] ; then
|
|
showHelp
|
|
elif [ "${1}" == "-g" ] ; then
|
|
createConfig
|
|
# Init
|
|
elif [ "${1}" == "-i" ] ; then
|
|
# Start script
|
|
createConfig
|
|
user=$(cat ${dirConfig}/ks-upload-user)
|
|
raw_password=$(cat ${dirConfig}/ks-upload-pass)
|
|
password=$(decrypt_kstools "${raw_password}")
|
|
server=$(cat ${dirConfig}/ks-upload-server)
|
|
dirServer=$(cat ${dirConfig}/ks-upload-dirserver)
|
|
dirLocal=$(cat ${dirConfig}/ks-upload-dirlocal)
|
|
# Call the functions to perform the whole process.
|
|
echo -n "* Scanning ${dirLocal} "
|
|
echo ""
|
|
current_tool=$(cat ${dirConfig}/ks-upload-tool)
|
|
if [ "${current_tool}" == "scp" ] ; then
|
|
echo ""
|
|
echo "* NOTE: You are using 'scp' to upload files and no progress will be shown."
|
|
echo "* IMPORTANT: It's possible switch to 'rsync' or 'scp-e' if you wish (Command: ks-upr -e)"
|
|
fi
|
|
if [ "${current_tool}" == "scp-rsa" ] ; then
|
|
echo ""
|
|
echo "* NOTE: You are using 'scp-rsa' to upload files and no progress will be shown."
|
|
echo "* IMPORTANT: It's possible switch to 'rsync-rsa' if you wish (Command: ks-upr -e)"
|
|
fi
|
|
if [ -d ${dirLocal} ] ; then
|
|
sendFile ${password} "${dirLocal}" ${user} ${server} ${dirServer}
|
|
echo ""
|
|
recursive_checksum ${password} "${dirLocal}" ${user} ${server} ${dirServer}
|
|
count=$(expr ${count} + 1)
|
|
else
|
|
echo "* Directory ${dirLocal} does not exist!"
|
|
echo ""
|
|
exit
|
|
fi
|
|
# Show help
|
|
else
|
|
showHelp
|
|
fi
|