Skip to content

Instantly share code, notes, and snippets.

@afifhusnul
Created August 9, 2020 10:37
Show Gist options
  • Save afifhusnul/a9f1b3df6619d2be039e1b446602681a to your computer and use it in GitHub Desktop.
Save afifhusnul/a9f1b3df6619d2be039e1b446602681a to your computer and use it in GitHub Desktop.
Decrypt tool
#!/bin/sh
## Settle your login 172.xxx.xxx.xxx (SSH/SFTP KEYLESS)
## Follow the step from this url : https://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps
## create a new folder RAW, INPUT_RAW, OUTPUT_RAW --> (mkdir - p RAW)
## Put your file into /home/$USER/INPUT_RAW
## This script will cut file from 1rst column into 1 piece to Decrypt
## To send into /opt/link/mediation/decrypt/input
## How to execute --> thisScrIpt.sh <user> <[email protected]>
log_dttm=`date +"%Y%m%d_%H%M%S"`
##--------------- Define the stuff
RCPT=$2
HOSTCMP=172.xxx.xxx.xxx
USER=USER_TXXXXXX
dirRaw='/home/'$USER'/RAW'
dirInput='/home/'$USER'/INPUT_RAW'
dirOutput='/home/'$USER'/OUTPUT_RAW'
inputFile=$dirInput/inputDecrypt.csv
outputFile=$dirOutput/$1_output_$log_dttm.csv
balanceFile=$dirRaw/balance_result.csv
resultFile=$dirRaw/$1_final_result_$log_dttm.csv
resultFileZip=$dirRaw/$1_final_result_$log_dttm.zip
pipeInputFile=$dirRaw/pipeInput.txt
pipeBalanceFile=$dirRaw/pipeBalance.txt
procFolder=/opt/link/mediation/decrypt/input
resFolder=/opt/link/mediation/decrypt/output
remoteResultFile=$resFolder/$1_output_$log_dttm.csv
##------------- Function upload file to REMOTE CMP server
function uploadFile() {
sftp $HOSTCMP <<EOF
mput $outputFile $procFolder
chmod 667 $procFolder/*output_$log_dttm.csv
bye
EOF
echo "File uploaded into CMP server"
}
##------------- Function download file to UAT server from REMOTE CMP server
function downloadFile() {
sftp $HOSTCMP <<EOF
mget $remoteResultFile $dirRaw
bye
EOF
echo "File has been downloaded sucessfully"
}
##------------- Function Search file in REMOTE CMP server
function checkFile() {
echo "ls -l $remoteResultFile" | sftp -b - $HOSTCMP
if [ $? -eq 0 ]
then
echo " File exists"
downloadFile
else
echo "File does not exist and need to wait for about 10 minutes to complete the decrypt process"
sleep 10m
if [ $? -eq 1 ]
then
echo "File exist"
downloadFile
else
echo "File still not exist and need to wait about 10 minutes more to complete the decrypt process"
sleep 10m
downloadFile
fi
fi
}
##------------- Function to send result file via email
function sendMail() {
ssh $USER@$HOSTCMP /bin/sh <<EOF
echo "File result in attachment" | /bin/mail -s "CMP Decrypt file result" -a $remoteResultFile -r "Alert Dcc<[email protected]>" $RCPT
EOF
}
#---------------- Move file name to default name
if [ "$(ls -A $dirInput)" ]; then
mv -f $dirInput/* $dirInput/inputDecrypt.csv
#---------------- Cut file into single column
cat $inputFile | awk -F";" '{print$1}' > $outputFile
cat $inputFile | awk -F";" -v OFS=';' '{ $1=""; print}' > $balanceFile
##--------------- Upload file to CMP server 172.31.7.112
uploadFile
echo "Waiting Decrypt process for about 5 minutes...."
##--------------- Upload file DONE & Delete original inputFile then slepp for 10 minutes
rm $dirInput/*
sleep 5m
##--------------- Check File in REMOTE folder is exist or not
checkFile
##--------------- Combine it into single file
paste -d ";" $dirRaw/$1_output_$log_dttm.csv $balanceFile > $resultFile
sleep 1m
SIZE=$(du -sb $resultFile | awk '{ print $1 }')
##---------------- Send result file if size <=3MB
if ((SIZE<3145728)) ; then
echo "Size are OK to send file via email"
sendMail
else
echo "Sorry, your file size too big to send via email, check your folder to get the result"
fi
##---------------- Clean up
rm $outputFile
rm $balanceFile
echo "--------------------------- Decryption file done ------------------------------------"
else
echo "Folder is empty, no file to process"
fi
Bash script to decrypt a file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment