Skip to content

Instantly share code, notes, and snippets.

@philipmat
Created September 14, 2011 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philipmat/1215820 to your computer and use it in GitHub Desktop.
Save philipmat/1215820 to your computer and use it in GitHub Desktop.
transmission cli for freenas
#!/bin/bash
# filename: transmission_cli_manager.sh
# author: Dan Merschi
# date: 2009-01-17 (0.69 rev.4260)
# update: 2009-03-01 (0.69 rev 4403)
# purpose: A fast and easy solution(script) to manage Transmission daemon via transmission-remote client
# source: http://sourceforge.net/apps/phpbb/freenas/viewtopic.php?f=12&t=822
#
if [ -z "$(pgrep transmission-daemon)" ] ;then echo Transmission not running, abort.; exit 3; fi
eval $( ps axww | \
awk -F'--' '/[t]ransmission-daemon/{
for(i=3;i++<8;){
split($i,_," ")
split(_[1],l,"-")
printf "%s=\"%s\"\n",toupper(l[1]l[2]),_[2]
}}')
TR="/usr/local/bin/transmission-remote localhost:${PORT} -n ${USERNAME}:${PASSWORD}"
function _list_torrents() {
if [ "${torrents:-"all"}" = "all" ];then
${TR} -l
else
for i in $torrents
do
${TR} -t $i -i
done
fi
}
function _start_torrents() {
if [ "${torrents:-"all"}" = "all" ];then
for i in $(${TR} -l | awk 'NR>1 && $7=="Stopped"{print $1}')
do
${TR} -t $i -s
done
else
for i in $torrents;
do
${TR} -t $i -s
done
fi
}
function _stop_torrents() {
if [ "${torrents:-"all"}" = "all" ];then
for i in $(${TR} -l | awk 'NR>1 && $7!="Stopped"{print $1}')
do
${TR} -t $i -S
done
else
for i in $torrents
do
${TR} -t $i -S
done
fi
}
function _stop_over_ratio() { # Stop each torent if required
torrent_list=$(${TR} -l)
while read ID Done Have MB ETA Up Down Ratio Status Name
do
if [ ${ETA} = "Done" ] && [ ${Status} != "Stopped" ] && [ ${Ratio/./} -ge ${ratio%.*} ]; then
argument="${argument} -t $ID -S "
fi
done <<< "${torrent_list}"
if [ ! -z "${argument}" ];then
${TR} ${argument} ;
fi
}
function _blocklist_update() {
URL=http://download.m0k.org/transmission/files/level1.gz
cd ${CONFIGDIR}blocklists/
# Get local file size
LocalSize=`stat -qf %z ${URL##*/}`
# Get remote file size or exit
RemoteSize=`fetch -apsw 5 ${URL} || exit 1;`
# If local file size not equal to remote file size update blocklist
if [ ${LocalSize:-0} -ne ${RemoteSize} ]; then
fetch -apw 5 $URL && \
gzip -dfkq ${URL##*/} && \
/etc/rc.d/transmission restart
echo "Blocklist updated successfully."
echo "OK Blocklists updated on: $(date)" >> $CONFIGDIR/transmission_blocklists.log
else
echo "NOK Update not required: $(date)" >> $CONFIGDIR/transmission_blocklists.log
echo "Update not required. You already have the latest blocklist file."
fi
}
function _scan_watchdir() {
if [ ${watchdir:-"null"} = "null" -o ! -d ${watchdir} ]; then
echo -e "Transmission watch directory not set or don't exit.";
else
for i in "$(ls $watchdir/*.torrent 2>/dev/null)";
do
${TR} -a "$i";
rm "$i";
done
fi
}
function _usage() { echo "
A fast and easy solution(script) to manage Transmission daemon via transmission-remote client.
Usage:
$0 -c [command] -{r|t} [options] -h {for help}
-c [command]: start : Start all torrents
start -t 1 : Start torrent 1
start -t 1 -t 3 : Start torrent 1 and 3
stop : Stop all torrents
stop -t 1 : Stop torrent 1
stop -t 1 -t 3 : Stop torrent 1 and 3
list : List all torrents
list -t 1 : List torrent 1
list -t 1 -t 3 : List torrent 1 and 3
ratio : Stop all torrents over 1:1 ratio
ratio -r 1.2 : Stop all torrents over [argument] ratio.
update : Update blocklist
Options:
-h Help (this message).
-r [val] Ratio limit (default 1:1).
-t [val] Torrent entry.
Last update: $ 2009-03-01 $
"
}
while getopts "c:d:r:t:h" OPTION
do
case $OPTION in
c) cmd=$OPTARG;;
d) watchdir=$OPTARG;;
h) _usage; exit 0;;
r) ratio=$(echo $OPTARG \* 100 | bc);;
t) torrents="$torrents $OPTARG";;
*) echo -e "Unknown command:\n\t\tUsage: $0 -h (for help)"; exit 2;;
esac
done
ratio=${ratio:-"1.10"};
watchdir=${watchdir:-"null"};
case $cmd in
add) _scan_watchdir; exit 0;;
update) _blocklist_update; exit 0;;
ratio) _stop_over_ratio;exit 0;;
list) _list_torrents; exit 0;;
help) _usage;exit 0;;
start) _start_torrents; exit 0;;
stop) _stop_torrents; exit 0;;
*) echo -e "Unknown command:\n\t\tUsage: $0 -h (for help)"; exit 2;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment