Create “myfile” full of random data to have an upload file
head -c 10M </dev/urandom >myfile
Create “serverlist” with servers to test, one line per server, no blank lines or comments
Create “mail.template” with your body header. This file is put at the top of the email if needed
Here is the script:
#!/bin/bash FILE=/home/filemon/myfile LOG=/home/filemon/log/sftpmon.log T_FILE=`mktemp` while read server do result=`scp -B -q $FILE $server:/ >/dev/null 2>&1; echo $?` if [ $result -eq 0 ] then echo "`date` $server successful" >> $LOG else echo "`date` $server failed" >> $LOG echo $server >> $T_FILE fi done < /home/filemon/serverlist if [ `cat $T_FILE | wc -l ` -gt 0 ] then cat /home/filemon/mail.template $T_FILE | mailx -s "SFTP server issue" [email protected] fi rm $T_FILE
Run this in cron with flock to prevent multiple instances
*/5 * * * * flock -n /home/filemon/sftpmon.lck -c /home/filemon/sftpmon.sh
Put the log file into logrotate to keep it under control.