I'm trying to write a simple script to scan (clamav) all files that were downloaded up on completion.
For most torrents having the torrent name and download directory is enough, however if the torrent name is not the same as the file or the directory of the content, I am forced to scan the entire downloads folder just to make sure everything was scanned.
So I'm wondering if there is an easy way to get access to a file list for the just completed torrent.
The only way i can see it working at the moment is generating a file list for every active torrent every X minutes and then using those file lists in the on-completion-script,
however I don't like that approach much, if needed i will implement it this way, but I would prefer having direct access to a filename-array of some sort directly in the on-completion script.
Is such an array available? Is it possible to implement it ?
The only variables that I have found any mention of on the web are :
TR_TORRENT_DIR
TR_TORRENT_NAME
TR_TORRENT_ID
TR_TORRENT_HASH
TR_TIME_LOCALTIME
TR_APP_VERSION
As soon as I have done a bit more polishing I will post the script here so others can use it too.
Accessing torrent file list in the on-completion script
-
- Posts: 3
- Joined: Tue Nov 02, 2010 9:21 pm
Re: Accessing torrent file list in the on-completion script
Something like :
Which means all the information is there, be careful using transmission-remote or your script will block, since scanning can take a long time everything is run in the background.
Code: Select all
#!/bin/sh
{
# Go to the download directory (the file list is relative to that point)
cd \"$TR_TORRENT_DIR\"
# Get a list of files (it will include directory/file or similar to any depth)
transmission-remote -t $TR_TORRENT_ID --info-files |
tail -n +3 | cut -c 35- |
# Process the list
while read f
do
clamdscan \"$f\"
done
} &
-
- Posts: 3
- Joined: Tue Nov 02, 2010 9:21 pm
Re: Accessing torrent file list in the on-completion script
Cheers, kinda stupid of me to forget I could use the torrent id to access the specific filelist !
I'll try to post the entire script after work tonight
I'll try to post the entire script after work tonight
-
- Posts: 3
- Joined: Tue Nov 02, 2010 9:21 pm
Re: Accessing torrent file list in the on-completion script
#!/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
Feedback is always greatly appreciated !
#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
Feedback is always greatly appreciated !