Linux Shell Script To Monitor Ftp Server Connection Main goal of this script is to monitor ftp server. This script is example of how to use ftp command in bash shell. System administrator can use this script to check if ftp server alive and upload there any data/backups. Everytime script create new file, every time with with different md5 sum, after script upload it to ftp server, check md5sum of both files. If md5sum same, that mean that file uploaded to ftpserver successfully. Source : http://linoxide.com/linux-shell-script/monitor-ftp-server/ #!/bin/bash Linux Shell 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.. $# number of parameters passed to script 1
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/ 2
LOG_FILE= /tmp/ftp.log EMAIL= yevhen.duma@gmail.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 3
#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 4
#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. 5
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./ftp.sh ftpserver ftp.example.com 20 Uploading file DONE Checking MD5 sum MD5 sum OK Linux Shell Script Result 6