Skip to content

Instantly share code, notes, and snippets.

@bulljit
Created July 17, 2011 13:05
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bulljit/1087570 to your computer and use it in GitHub Desktop.
Save bulljit/1087570 to your computer and use it in GitHub Desktop.
Transmission-Daemon Script: OnComplete ClamAV
#!/bin/sh
#user or email the logs get mailed to in case of error / virus (if your mta supports non-local email)
ADMIN="user"
#Log file used, warning, if you re-use the log file all previous data will be overwritten each time the script is run.
LOGFILE="/var/log/transmission/scanlogs/transmission-completed-$TR_TORRENT_NAME.log"
#Boolean to choose if logs should be deleted when no error occured or virus was found
DELETE_LOG_FILES_ON_OK=false
#Boolean to choose if logs should be mailed when no error occured or virus was found
MAIL_LOG_FILES_ON_OK=false
#Boolean to choose if you want to use authentication for retrieving the file list from transmission
AUTHENTICATE=true
#Transmission details required for authentication
HOST=localhost
PORT=9091
USERNAME=transmission-user
PASSWORD=transmission-password
echo transmission-torrent-complete.sh running on `date` > "$LOGFILE"
echo Directory is "$TR_TORRENT_DIR" >> "$LOGFILE"
echo Torrent Name is "$TR_TORRENT_NAME" >> "$LOGFILE"
echo Torrent ID is "$TR_TORRENT_ID" >> "$LOGFILE"
echo Torrent Hash is "$TR_TORRENT_HASH" >> "$LOGFILE"
# Get a list of files (it will include directory/file or similar to any depth)
if $AUTHENTICATE
then
FILES=$(transmission-remote $HOST:$PORT --auth=$USERNAME:$PASSWORD -t $TR_TORRENT_ID -f | tail -n +3 | cut -c 35-)
else
FILES=$(transmission-remote -t "$TR_TORRENT_ID" -f | tail -n +3 | cut -c 35-)
fi
# Go to the download directory (the file list is relative to that point)
cd "$TR_TORRENT_DIR/"
#Moving the filelist into a temporary file wich we can pass on to clamav, using all file names inline gave too many problems regarding special characters
TEMPFILE=`mktemp /tmp/transmission-torrent-complete-secureXXXXXXXX`
echo "$FILES" > $TEMPFILE
#We scan our files and we are verbose about it in case of errors, we grep out the "scanning" lines since only the result is interesting to our logfile
`clamscan -vf $TEMPFILE | grep -ve Scanning* >> "$LOGFILE"`
#We note the exit value of the scan
RESULT=$?
#Casing our result into : 0/1/2/default
#0 -> no problem found, 1 -> a virus was found, 2 -> errors occurred during the scan, other value -> a serious error occurred
#0 -> we delete our log file, 1&2 -> we warn user about the found issue entire log file gets appended to the mail so he can remove the virus or debug
case $RESULT in
0)
if $DELETE_LOG_FILES_ON_OK
then
rm "$LOGFILE"
else
echo "No Virus found, no issues to report" >> "$LOGFILE"
if $MAIL_LOG_FILES_ON_OK
then
/usr/bin/mail -s "Torrent $TR_TORRENT_NAME checked out OK" "$ADMIN" < "$LOGFILE"
fi
fi
;;
1)
echo "Virus Found !! Warning $ADMIN" >> "$LOGFILE"
/usr/bin/mail -s "Virus Found while scanning $TR_TORRENT_NAME" "$ADMIN" < "$LOGFILE"
exit 1
;;
2)
echo "Error Occurred!! Warning $ADMIN" >> "$LOGFILE"
/usr/bin/mail -s "Error Occurred while scanning $TR_TORRENT_NAME" "$ADMIN" < "$LOGFILE"
exit 1
;;
*)
echo "Return value of scanning was not 0,1 or 2, result was, $RESULT , unknown problematic situation" >> "$LOGFILE"
/usr/bin/mail -s "Unknown value returned from scanning $TR_TORRENT_NAME" "$ADMIN" < "$LOGFILE"
exit 1
;;
esac
exit 0
@bulljit
Copy link
Author

bulljit commented Jul 17, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment