Linux menu

Wednesday, September 24, 2014

Linux Shell Script To Monitor Ftp Server Connection

#!/bin/bash
function usage {
echo “Usage: $0″
echo “–ftpserver 
echo “–help – this help”
exit 1
}
if [ $# -eq 0 ]; then
usage
fi
while [ $# -gt 0 ]
do
case “$1″ in
–ftpserver) HOST=$2;TIMEOUT=$3;shift;;
–help) usage;;
*) break;;
esac
shift
done
if [ x$HOST = "x" ]; then
usage
fi
### Global Variables
ORIG_DIR=”/tmp/”
LOG_FILE=”/tmp/ftp.log”
EMAIL=”info@linoxide.com”
#next function will make all checks.
function check {
#few more variables
####VARIABLES
MAXTIMEOUT=300
ORIG_FILE=”check.file”
FTPTIMEOUT=’20’
USERNAME=”user”
FOLDER=”test”
PASSWORD=”password”
DATAFOLDER=”somefolder”
cd $ORIG_DIR
echo -n “Creating file … ”
dd if=/dev/urandom of=$ORIG_DIR/$ORIG_FILE bs=104857 count=150 > /dev/null 2>&1
echo “Done”
echo -n “Uploading file … ”
ftp -inv < $LOG_FILE
open $HOST
user $USERNAME $PASSWORD
cd $FOLDER/$DATAFOLDER
binary
passive
put $ORIG_FILE
quote size $ORIG_FILE
close
bye
EOF
echo ” DONE”
echo -n “Checking MD5 sum … ”
ftp -inv <> $LOG_FILE
open $HOST
user $USERNAME $PASSWORD
cd $FOLDER/$DATAFOLDER
passive
binary
quote size $ORIG_FILE
dir
quote XMD5 $ORIG_FILE
close
bye
EOF
MD5=`tail -2 $LOG_FILE | head -1 | awk ‘{print $2}’ | tr [:upper:] [:lower:]`
ftp -in </dev/null
open $HOST
user $USERNAME $PASSWORD
cd $FOLDER/$DATAFOLDER
delete $ORIG_FILE
close
bye
EOF
MD5_ORIG=`/usr/bin/md5sum $ORIG_FILE | awk ‘{print $1}’`
if [ x"${MD5}" != x"${MD5_ORIG}" ]; then
RESULT=” File corrupted.”
else
RESULT=” MD5 sum OK ”
fi
echo $RESULT
rm $ORIG_FILE
}
check

Linux Shell Script Output

./ftp.sh –ftpserver ftp.example.com 20
Uploading file … DONE
Checking MD5 sum … MD5 sum OK

Understanding the above script

#Since there will be few times of using same commands, we will create function with name usage
#This function just will show us help menu and will exit from script.
function usage {
echo “Usage: $0″
#we tell user which arguments script expect
echo “–ftpserver 
echo “–help – this help”
#since not argument of bad argument – exit from script
exit 1
}
#We check if user pass parameters to script
if [ $# -eq 0 ]; then
#if no parameters just run function usage that show help menu
usage
fi
while [ $# -gt 0 ] #if user pass some parameters we check each
do
case “$1″ in #we take first argument passed by user
#if user pass –ftpserver as first parameter, so second will be ftp servername and third timeout. 
–ftpserver) HOST=$2;TIMEOUT=$3;shift;;
#if user pass –help as parameter we just output help menu
–help) usage;;
#if anything else passed – just exit from script
*) break;;
esac
shift
done #finish of while loop
#if user didn’t pass ftp servername – we show help menu
if [ x$HOST = "x" ]; then
usage
fi
## We will use some variables, so in next lines just putting their values.
### Global Variables
ORIG_DIR=”/tmp/”
LOG_FILE=”/tmp/ftp.log”
EMAIL=”info@linoxide.com”
#next function will make all checks.
function check {
#few more variables
####VARIABLES
MAXTIMEOUT=300
ORIG_FILE=”check.file”
FTPTIMEOUT=’20’
USERNAME=”user”
FOLDER=”test”
PASSWORD=”password”
DATAFOLDER=”somefolder”
#entering temp folder
cd $ORIG_DIR
#with dd we generate 15Mb file with name from $ORIG_FILE variable and with random content.
echo -n “Creating file … ”
dd if=/dev/urandom of=$ORIG_DIR/$ORIG_FILE bs=104857 count=150 > /dev/null 2>&1
echo “Done”
# This tell user that we starting to upload file
echo -n “Uploading file … ”
#we use ftp command to upload file, -i turns off interactive prompting 
ftp -inv < $LOG_FILE
#connect to host
open $HOST
#connect with user and password
user $USERNAME $PASSWORD
#cd to some test folder
cd $FOLDER/$DATAFOLDER
#we tell ftp server that we will use binary mode
binary
#we will use passive mode
passive
#this command copy local file to ftp server
put $ORIG_FILE
#getting size of remove file
quote size $ORIG_FILE
#closing connection
close
bye
#exit from ftp command
EOF
#telling user that upload done
echo ” DONE”
#starting md5 check
echo -n “Checking MD5 sum … ”
#same as above 
ftp -inv <> $LOG_FILE
open $HOST
user $USERNAME $PASSWORD
cd $FOLDER/$DATAFOLDER
passive
binary
quote size $ORIG_FILE
dir
#getting md5 of remote file
quote XMD5 $ORIG_FILE
close
bye
EOF
#getting md5sum value from log file, since it will be in upper case we convert it to lower case with tr
MD5=`tail -2 $LOG_FILE | head -1 | awk ‘{print $2}’ | tr [:upper:] [:lower:]`
#we need to connect to remote server once more to delete file
ftp -in </dev/null
open $HOST
user $USERNAME $PASSWORD
cd $FOLDER/$DATAFOLDER
#this command delete remote file
delete $ORIG_FILE
close
bye
EOF
#with md5sum command we get md5sum of local file
MD5_ORIG=`/usr/bin/md5sum $ORIG_FILE | awk ‘{print $1}’`
#this line compare md5sums, and variable RESULT will contain message according to check
if [ x"${MD5}" != x"${MD5_ORIG}" ]; then
RESULT=” File corrupted.”
else
RESULT=” MD5 sum OK ”
fi
#this will tell user if file was uploaded successfully or not.
echo $RESULT
#deleting local file
rm $ORIG_FILE
#end of function
}
#to run function (without arguments)
check

No comments: