Last active
December 23, 2021 16:34
-
-
Save r4ulcl/0c0e8129a7d26676b4e52295e1983a26 to your computer and use it in GitHub Desktop.
Script that downloads files with Transmission and saves all peers in a sqlite3 database.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#author : Raul Calvo Laorden ([email protected]) | |
#description : Script that downloads files with Transmission and saves all peers in a sqlite3 database. | |
#date : 2018-03-26 | |
#usage : bash torrentDaemon.sh [options] | |
#----------------------------------------------------------------------------------------------------------- | |
PCENTMAX=8 #8% max | |
function help(){ | |
echo -e "author : Raul Calvo Laorden ([email protected])" | |
echo -e "description : Script that downloads files with Transmission and saves all peers in a sqlite3 database." | |
echo -e "usage : bash torrentDaemon.sh [options]\n" | |
echo -e "\nOptions: " | |
echo -e "\t -a \t --add \t <hash> \t Add hash to Transmission" | |
echo -e "\t -aF \t --addFile \t <file> \t Add file to Transmission" | |
echo -e "\t -d \t --data \t \t Show ips and % of all files in Transmission" | |
echo -e "\t -h \t --help \t \t Show this message" | |
echo -e "\t -i \t --info \t <name> \t Show all data in DB of file with hash <hash>" | |
echo -e "\t -l \t --list \t \t List all files in Transmission" | |
echo -e "\t -r \t --remove \t <ID> \t Remove file with id <ID> from Transmission" | |
echo -e "\t -rA \t --removeAll \t \t Remove all from Transmission" | |
echo -e "\t -s \t --start \t \t Start script" | |
echo -e "\t -S \t --stop \t \t Stop Transmission and kill script" | |
} | |
#remove all torrents -gt PCENTMAX and add it again | |
function checkMax(){ | |
oldIFS=$IFS | |
IFS=$'\n' | |
LIST=$(transmission-remote -l 2> /dev/null | grep -v -E "ID|Sum") | |
for i in $LIST ; do | |
ID=$(echo $i | awk '{print $1;}') | |
NAME=$(echo $i | awk '{print $NF;}') | |
PCENT=$(echo $i | awk '{print $2;}' | sed 's/%//') #eliminamos el % | |
#STATUS=$(echo $i | awk '{print $(NF-1);}') | |
if [ $PCENT != "n/a" ]; then | |
if [ $PCENT -gt $PCENTMAX ] | |
then | |
HASH=$(transmission-remote -t $ID -i | grep Hash: | awk '{print $2}' ) | |
transmission-remote -t $ID -rad >> /dev/null #delete | |
add $HASH | |
fi | |
fi | |
done | |
} | |
#add hash to transmission download | |
function add(){ | |
transmission-remote -a magnet:?xt=urn:btih:$1 >> /dev/null | |
} | |
#add .torrent to transmission download | |
function addFile(){ | |
transmission-remote -a $1 >> /dev/null | |
} | |
#start the daemon in background | |
function start(){ | |
if [ "$1" != "calling_myself" ] | |
then | |
stop | |
"$0" "calling_myself" $@ & | |
exit $? | |
fi | |
transmission-daemon -L 10000000 -l 100000 #-e logfile.log | |
#-L --peerlimit-global limit -l --peerlimit-torrent limit | |
sleep 3 | |
transmission-remote -u 1000 >> /dev/null | |
transmission-remote -d 1000 >> /dev/null | |
IFS=$'\n' | |
LIST=$(transmission-remote -l 2> /dev/null | grep -v -E "ID|Sum") | |
for i in $LIST ; do | |
ID=$(echo $i | awk '{print $1;}') | |
transmission-remote -t $ID -s >> /dev/null #start all torrents | |
done | |
checkMax | |
CONTADOR=0 | |
while [ true ] ; do | |
checkMax | |
#List the specified torrent's peers | |
IFS=$'\n' | |
LIST=$(transmission-remote -l 2> /dev/null | grep -v -E "ID|Sum") | |
for i in $LIST ; do | |
ID=$(echo $i | awk '{print $1;}') | |
NAME=$(echo $i | awk '{print $NF;}') | |
PCENT=$(echo $i | awk '{print $2;}' | sed 's/%//') #% | |
if [ $PCENT != "n/a" ]; then | |
#Creamos base de datos | |
sqlite3 db-$NAME.sqlite "CREATE TABLE IF NOT EXISTS ips(ip TEXT NOT NULL PRIMARY KEY UNIQUE, flags TEXT, client TEXT);" | |
sqlite3 db-$NAME.sqlite "CREATE TABLE IF NOT EXISTS date(ip TEXT NOT NULL, date DATE NOT NULL, PRIMARY KEY (ip, date));" | |
#data to DB | |
AUX=$(transmission-remote -t $ID -ip | grep -v "Address") | |
for ip in $AUX ; do | |
IP=$(echo $ip | awk '{print $1;}') | |
DATE=$(date +"%a %b %d %R CET %G") #no sec | |
FLAGS=$(echo $ip | awk '{print $2;}') | |
CLIENT=$(echo $ip | awk '{print $6 $7 $8 $9;}') | |
sqlite3 db-$NAME.sqlite "INSERT OR REPLACE INTO ips VALUES ('$IP','$FLAGS', '$CLIENT');" | |
sqlite3 db-$NAME.sqlite "INSERT OR REPLACE INTO date VALUES ('$IP', '$DATE');" | |
done | |
#Reannounce torrent | |
AUX=$((CONTADOR%100)) | |
if [ $AUX -eq 0 ] | |
then | |
transmission-remote -t $ID --reannounce >> /dev/null | |
fi | |
fi | |
done | |
sleep 1 | |
let "CONTADOR+=1" | |
done | |
} | |
#stop the torrents in transmission, stop and kill transmission and kill the script in background | |
function stop(){ | |
IFS=$'\n' | |
#Stop torrents | |
LIST=$(transmission-remote -l 2> /dev/null | grep -v -E "ID|Sum") | |
for i in $LIST ; do | |
ID=$(echo $i | awk '{print $1;}') | |
transmission-remote -t $ID -S 2> /dev/null | |
done | |
#ps aux | grep transmission-daemon | |
LIST=$(ps aux | grep transmission-daemon) | |
for i in $LIST ; do | |
ID=$(echo $i | awk '{print $2;}') | |
kill $ID 2> /dev/null | |
done | |
#kill scripts | |
LIST=$(ps aux | grep calling_myself) | |
for i in $LIST ; do | |
ID=$(echo $i | awk '{print $2;}') | |
kill $ID 2> /dev/null | |
done | |
} | |
#remove and delete 1 torrent | |
function remove(){ | |
transmission-remote -t $1 -rad >> /dev/null | |
} | |
#remove and delete all torrents | |
function removeAll(){ | |
IFS=$'\n' | |
LIST=$(transmission-remote -l 2> /dev/null | grep -v -E "ID|Sum") | |
for i in $LIST ; do | |
ID=$(echo $i | awk '{print $1;}') | |
transmission-remote -t $ID -rad | |
done | |
} | |
function list(){ | |
transmission-remote -l 2> /dev/null | grep -v -E "ID|Sum" | |
} | |
#Prints the number of IPs and the percentage downloaded for all files | |
function data(){ | |
IFS=$'\n' | |
LIST=$(transmission-remote -l 2> /dev/null | grep -v -E "ID|Sum") | |
for i in $LIST ; do | |
ID=$(echo $i | awk '{print $1;}') | |
NAME=$(echo $i | awk '{print $NF;}') | |
if [ -n "$NAME" ]; then | |
PCENT=$(echo $i | awk '{print $2;}' | sed 's/%//') #eliminamos el % | |
LINEAS=$(sqlite3 db-$NAME.sqlite "SELECT * from ips;" | wc -l) | |
echo -e "Number of IPs $NAME = $LINEAS and $PCENT % " | |
fi | |
done | |
} | |
#Select * from ip | |
function info(){ | |
sqlite3 db-$1.sqlite "SELECT * from ips;" | |
} | |
#variables para cambiar de color del echo | |
RED=`tput setaf 1` | |
GREEN=`tput setaf 2` | |
NC=`tput sgr0` | |
MYARRAY=( "$@" ) | |
for BUCLE in "$@" | |
do | |
case $BUCLE in | |
-a|--add) add ${MYARRAY[($BUCLE)+1]};; | |
aF|--addFile) addFile ${MYARRAY[($BUCLE)+1]};; | |
-s|--start) start $@ ;; | |
-S|--stop) stop ; exit 0;; | |
-r|--remove) remove ${MYARRAY[($BUCLE)+1]};; | |
-rA|--removeAll) removeAll;; | |
-d|--data) data;; | |
-i|--info) info ${MYARRAY[($BUCLE)+1]};; | |
-l|--list) list;; | |
-h|--help) help;; | |
esac | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment