· 8 years ago · Apr 04, 2018, 04:16 PM
1#!/bin/bash
2
3#author : Raul Calvo Laorden (raulcalvolaorden@gmail.com)
4#description : Script that downloads files with Transmission and saves all peers in a sqlite3 database.
5#date : 2018-03-26
6#usage : bash torrentDaemon.sh [options]
7#-----------------------------------------------------------------------------------------------------------
8
9PCENTMAX=8 #8% max
10
11function help(){
12 echo -e "author : Raul Calvo Laorden (raulcalvolaorden@gmail.com)"
13 echo -e "description : Script that downloads files with Transmission and saves all peers in a sqlite3 database."
14 echo -e "usage : bash torrentDaemon.sh [options]\n"
15
16 echo -e "\nOptions: "
17 echo -e "\t -a \t --add \t <hash> \t Add hash to Transmission"
18 echo -e "\t -aF \t --addFile \t <file> \t Add file to Transmission"
19 echo -e "\t -d \t --data \t \t Show ips and % of all files in Transmission"
20 echo -e "\t -h \t --help \t \t Show this message"
21 echo -e "\t -i \t --info \t <name> \t Show all data in DB of file with hash <hash>"
22 echo -e "\t -l \t --list \t \t List all files in Transmission"
23 echo -e "\t -r \t --remove \t <ID> \t Remove file with id <ID> from Transmission"
24 echo -e "\t -rA \t --removeAll \t \t Remove all from Transmission"
25 echo -e "\t -s \t --start \t \t Start script"
26 echo -e "\t -S \t --stop \t \t Stop Transmission and kill script"
27}
28
29#remove all torrents -gt PCENTMAX and add it again
30function checkMax(){
31 oldIFS=$IFS
32 IFS=$'\n'
33
34 LIST=$(transmission-remote -l 2> /dev/null | grep -v -E "ID|Sum")
35
36 for i in $LIST ; do
37 ID=$(echo $i | awk '{print $1;}')
38 NAME=$(echo $i | awk '{print $NF;}')
39 PCENT=$(echo $i | awk '{print $2;}' | sed 's/%//') #eliminamos el %
40 #STATUS=$(echo $i | awk '{print $(NF-1);}')
41
42 if [ $PCENT != "n/a" ]; then
43
44 if [ $PCENT -gt $PCENTMAX ]
45 then
46 HASH=$(transmission-remote -t $ID -i | grep Hash: | awk '{print $2}' )
47 transmission-remote -t $ID -rad >> /dev/null #delete
48 add $HASH
49 fi
50 fi
51 done
52}
53
54
55
56#add hash to transmission download
57function add(){
58 transmission-remote -a magnet:?xt=urn:btih:$1 >> /dev/null
59}
60
61#add .torrent to transmission download
62function addFile(){
63 transmission-remote -a $1 >> /dev/null
64}
65
66#start the daemon in background
67function start(){
68 if [ "$1" != "calling_myself" ]
69 then
70 stop
71 "$0" "calling_myself" $@ &
72 exit $?
73 fi
74
75 transmission-daemon -L 10000000 -l 100000 #-e logfile.log
76 #-L --peerlimit-global limit -l --peerlimit-torrent limit
77
78 sleep 3
79
80 transmission-remote -u 1000 >> /dev/null
81 transmission-remote -d 1000 >> /dev/null
82
83 IFS=$'\n'
84
85 LIST=$(transmission-remote -l 2> /dev/null | grep -v -E "ID|Sum")
86 for i in $LIST ; do
87 ID=$(echo $i | awk '{print $1;}')
88 transmission-remote -t $ID -s >> /dev/null #start all torrents
89 done
90
91 checkMax
92 CONTADOR=0
93
94 while [ true ] ; do
95 checkMax
96 #List the specified torrent's peers
97 IFS=$'\n'
98 LIST=$(transmission-remote -l 2> /dev/null | grep -v -E "ID|Sum")
99
100 for i in $LIST ; do
101 ID=$(echo $i | awk '{print $1;}')
102 NAME=$(echo $i | awk '{print $NF;}')
103
104 PCENT=$(echo $i | awk '{print $2;}' | sed 's/%//') #%
105
106 if [ $PCENT != "n/a" ]; then
107 #Creamos base de datos
108 sqlite3 db-$NAME.sqlite "CREATE TABLE IF NOT EXISTS ips(ip TEXT NOT NULL PRIMARY KEY UNIQUE, flags TEXT, client TEXT);"
109 sqlite3 db-$NAME.sqlite "CREATE TABLE IF NOT EXISTS date(ip TEXT NOT NULL, date DATE NOT NULL, PRIMARY KEY (ip, date));"
110
111 #data to DB
112 AUX=$(transmission-remote -t $ID -ip | grep -v "Address")
113 for ip in $AUX ; do
114 IP=$(echo $ip | awk '{print $1;}')
115 DATE=$(date +"%a %b %d %R CET %G") #no sec
116 FLAGS=$(echo $ip | awk '{print $2;}')
117 CLIENT=$(echo $ip | awk '{print $6 $7 $8 $9;}')
118 sqlite3 db-$NAME.sqlite "INSERT OR REPLACE INTO ips VALUES ('$IP','$FLAGS', '$CLIENT');"
119 sqlite3 db-$NAME.sqlite "INSERT OR REPLACE INTO date VALUES ('$IP', '$DATE');"
120 done
121
122 #Reannounce torrent
123 AUX=$((CONTADOR%100))
124 if [ $AUX -eq 0 ]
125 then
126 transmission-remote -t $ID --reannounce >> /dev/null
127 fi
128
129 fi
130 done
131 sleep 1
132 let "CONTADOR+=1"
133 done
134}
135
136#stop the torrents in transmission, stop and kill transmission and kill the script in background
137function stop(){
138 IFS=$'\n'
139
140 #Stop torrents
141 LIST=$(transmission-remote -l 2> /dev/null | grep -v -E "ID|Sum")
142 for i in $LIST ; do
143 ID=$(echo $i | awk '{print $1;}')
144 transmission-remote -t $ID -S 2> /dev/null
145 done
146
147 #ps aux | grep transmission-daemon
148 LIST=$(ps aux | grep transmission-daemon)
149 for i in $LIST ; do
150 ID=$(echo $i | awk '{print $2;}')
151 kill $ID 2> /dev/null
152 done
153
154 #kill scripts
155 LIST=$(ps aux | grep calling_myself)
156 for i in $LIST ; do
157 ID=$(echo $i | awk '{print $2;}')
158 kill $ID 2> /dev/null
159 done
160}
161
162#remove and delete 1 torrent
163function remove(){
164 transmission-remote -t $1 -rad >> /dev/null
165}
166
167#remove and delete all torrents
168function removeAll(){
169 IFS=$'\n'
170 LIST=$(transmission-remote -l 2> /dev/null | grep -v -E "ID|Sum")
171
172 for i in $LIST ; do
173 ID=$(echo $i | awk '{print $1;}')
174 transmission-remote -t $ID -rad
175 done
176}
177
178function list(){
179 transmission-remote -l 2> /dev/null | grep -v -E "ID|Sum"
180}
181
182#Prints the number of IPs and the percentage downloaded for all files
183function data(){
184 IFS=$'\n'
185 LIST=$(transmission-remote -l 2> /dev/null | grep -v -E "ID|Sum")
186
187
188 for i in $LIST ; do
189 ID=$(echo $i | awk '{print $1;}')
190 NAME=$(echo $i | awk '{print $NF;}')
191
192 if [ -n "$NAME" ]; then
193 PCENT=$(echo $i | awk '{print $2;}' | sed 's/%//') #eliminamos el %
194 LINEAS=$(sqlite3 db-$NAME.sqlite "SELECT * from ips;" | wc -l)
195 echo -e "Number of IPs $NAME = $LINEAS and $PCENT % "
196 fi
197 done
198}
199
200#Select * from ip
201function info(){
202 sqlite3 db-$1.sqlite "SELECT * from ips;"
203}
204
205#variables para cambiar de color del echo
206RED=`tput setaf 1`
207GREEN=`tput setaf 2`
208NC=`tput sgr0`
209
210MYARRAY=( "$@" )
211
212for BUCLE in "$@"
213do
214 case $BUCLE in
215 -a|--add) add ${MYARRAY[($BUCLE)+1]};;
216 aF|--addFile) addFile ${MYARRAY[($BUCLE)+1]};;
217 -s|--start) start $@ ;;
218 -S|--stop) stop ; exit 0;;
219 -r|--remove) remove ${MYARRAY[($BUCLE)+1]};;
220 -rA|--removeAll) removeAll;;
221 -d|--data) data;;
222 -i|--info) info ${MYARRAY[($BUCLE)+1]};;
223 -l|--list) list;;
224 -h|--help) help;;
225 esac
226done
227exit 0