-
Notifications
You must be signed in to change notification settings - Fork 0
/
transmissionRemote.sh
230 lines (211 loc) · 8.86 KB
/
transmissionRemote.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#! /bin/bash
# This script will allow you to control your transmission jail via SSH from FreeNAS.
######################################
### ENABLE DEBUG MODE IF REQUESTED ###
######################################
debug=$( echo $@ | grep "\-\-debug" )
if [[ $debug ]]; then
echo -en "${bold}${yellow}Starting script in debug mode.. ${reset}"
set -x
fi
###############
###FUNCTIONS###
###############
function configSetup {
user=$( id | grep -o "(.*" | awk '{print $1}' | sed -e 's/(//' -e 's/)//' )
cfgLocation="/home/$user/.transmissionRemote.cfg"
echo -en "${bold}${green}Checking to make sure configuration file exists..${reset}"
if ! [[ -f "$cfgLocation" ]]; then
echo -e "${bold}${red}Error!${reset}"
echo -e "${bold}${yellow}Configuration file does not exist. Creating it..${reset}"
sudo mkdir /home 2> /dev/null
sudo mkdir /home/$user 2> /dev/null
sudo touch $cfgLocation
if ! [[ -f "$cfgLocation" ]]; then
echo -e "${bold}${red}Error!${reset}"
echo -e "${bold}${yellow}Unable to create configuration file $cfgLocation${reset}"
exit
fi
echo -en "${bold}${yellow}Changing ownership of $cfglocation.. ${reset}"
sudo chown $user $cfgLocation 2> /dev/null
echo -en "${bold}${yellow}Changing permissions of $cfglocation.. ${reset}"
chmod 600 $cfgLocation 2> /dev/null
echo -en "${bold}${yellow}What is your transmission web username? ${reset}"
read webUser
echo -en "${bold}${yellow}What is your transmission web password? ${reset}"
read webPass
echo -en "${bold}${yellow}What is your transmission jail name (Ex. transmission_1)? ${reset}"
read jailName
echo "## This file was automatically generated by $0 ##" > $cfgLocation
echo "Username: $webUser" >> $cfgLocation
echo "Password: $webPass" >> $cfgLocation
echo "JailName: $jailName" >> $cfgLocation
else
echo -e "${bold}${green}Ok!${reset}"
echo -en "${bold}${yellow}Checking $cfgLocation for username.. ${reset}"
webUser=$( cat $cfgLocation | grep "Username:" | sed -e 's/Username: //' )
if ! [[ $webUser ]]; then
echo -e "${bold}${red}Error!${reset}"
echo -e "${bold}${yellow}An error has been detected in your configuration file, $cfgLocation. We are unable to determine the transmission web username.${reset}"
while ! [[ $webUser ]]; do
echo -en "${bold}${yellow}What is your transmission web username? ${reset}"
read webUser
echo "Username: $webUser" >> $cfgLocation
done
else
echo -e "${bold}${green}Ok!${reset}"
fi
echo -en "${bold}${yellow}Checking $cfgLocation for password.. ${reset}"
webPass=$( cat $cfgLocation | grep "Password:" | sed -e 's/Password: //' )
if ! [[ $webPass ]]; then
echo -e "${bold}${red}Error!${reset}"
echo -e "${bold}${yellow}An error has been detected in your configuration file, $cfgLocation. We are unable to determine the transmission web password.${reset}"
while ! [[ $webPass ]]; do
echo -en "${bold}${yellow}What is your transmission web password? ${reset}"
read webPass
echo "Password: $webPass" >> $cfgLocation
done
else
echo -e "${bold}${green}Ok!${reset}"
fi
transmissionLogin="$webUser:$webPass"
echo -en "${bold}${yellow}Checking $cfgLocation for jail name.. ${reset}"
jailName=$( cat $cfgLocation | grep "JailName:" | sed -e 's/JailName: //' )
if ! [[ $jailName ]]; then
echo -e "${bold}${red}Error!${reset}"
echo -e "${bold}${yellow}An error has been detected in your configuration file, $cfgLocation. We are unable to determine the transmission jail name..${reset}"
while ! [[ $jailName ]]; do
echo -en "${bold}${yellow}What is your transmission jail name? ${reset}"
read jailName
echo "JailName: $jailName" >> $cfgLocation
done
else
echo -e "${bold}${green}Ok!${reset}"
fi
fi
}
function findJail {
jail=$( jls | grep $jailName | awk '{print $1}' )
}
function addTorrent {
sudo jexec "$jail" transmission-remote -n "$transmissionLogin" -a "$addLocation"
}
function listTorrents {
if [[ $1 ]]; then
torrent="$1"
checkExist=$( sudo jexec "$jail" transmission-remote -n "$transmissionLogin" -l | awk '{print $1}' | grep -v Sum | grep -v ID | grep "$torrent" )
if ! [[ $checkExist ]]; then
echo -e "${bold}${red}Error: You have entered an invalid torrent ID. Please choose from the following list:\n\n${reset}"
sudo jexec "$jail" transmission-remote -n "$transmissionLogin" -l
echo -en "${bold}${yellow}\nEnter a torrent ID: ${reset}"
read torrent
checkExist=$( sudo jexec "$jail" transmission-remote -n "$transmissionLogin" -l | awk '{print $1}' | grep -v Sum | grep -v ID | grep "$torrent" )
fi
while ! [[ $checkExist ]]; do
echo -en "${bold}${red}Error: You have entered an invalid torrent ID. Please try again: ${reset}"
read torrent
checkExist=$( sudo jexec "$jail" transmission-remote -n "$transmissionLogin" -l | awk '{print $1}' | grep -v Sum | grep -v ID | grep "$torrent" )
done
else
sudo jexec "$jail" transmission-remote -n "$transmissionLogin" -l
fi
}
function stopAllTorrents {
for torrent in $( sudo jexec "$jail" transmission-remote -n "$transmissionLogin" -l | awk {'print $1'} | grep -v Sum | grep -v ID ); do
stopTorrent $torrent
done
}
function startAllTorrents {
for torrent in $( sudo jexec "$jail" transmission-remote -n "$transmissionLogin" -l | awk {'print $1'} | grep -v Sum | grep -v ID ); do
startTorrent $torrent
done
}
function startTorrent {
if [[ $1 ]]; then
torrent="$1"
fi
echo -en "${bold}${yellow}Starting torrent ID $torrent.. ${reset}"
startResult=$( sudo jexec "$jail" transmission-remote -n "$transmissionLogin" -t $torrent -s )
checkSuccess=$( echo "$startResult" | grep -o "success" )
checkAuth=$( echo "$startResult" | grep -oi "unauthorized" )
if [[ $checkSuccess = "success" ]]; then
echo -e "${bold}${green}Ok!${reset}"
echo -e "${bold}${yellow}Successfully started $torrent.${reset}"
elif ! [[ $checkSuccess ]] && [[ $checkAuth = "unauthorized" ]]; then
echo -e "${bold}${red}Error!${reset}"
echo -e "${bold}${yellow}Unable to authorize with current credentials. Please change the credentials in the configuration file: $configFile${reset}"
else
echo -e "${bold}${red}Error!${reset}"
echo -e "${bold}${yellow}An unknown error has occurred.${reset}"
fi
}
function stopTorrent {
if [[ $1 ]]; then
torrent="$1"
fi
echo -en "${bold}${yellow}Stopping torrent ID $torrent.. ${reset}"
stopResult=$( sudo jexec "$jail" transmission-remote -n "$transmissionLogin" -t $torrent -S )
checkSuccess=$( echo "$stopResult" | grep -o "success" )
checkAuth=$( echo "$stopResult" | grep -oi "unauthorized" )
if [[ $checkSuccess = "success" ]]; then
echo -e "${bold}${green}Ok! ${reset}"
echo -e "${bold}${yellow}Successfully stopped $torrent.${reset}"
elif ! [[ $checkSuccess ]] && [[ $checkAuth = "unauthorized" ]]; then
echo -e "${bold}${red}Error!${reset}"
echo -e "${bold}${yellow}Unable to authorize with current credentials. Please change the credentials in the configuration file: $configFile${reset}"
else
echo -e "${bold}${red}Error!${reset}"
echo -e "${bold}${yellow}An unknown error has occurred.${reset}"
fi
}
function removeTorrent {
sudo jexec "$jail" transmission-remote -n "$transmissionLogin" -r -t $torrent
}
function removeAllTorrents {
sudo jexec "$jail" transmission-remote -n "$transmissionLogin" -r
}
### CHECK AND WRITE CONFIGURATION IF NECESSARY ###
configSetup
##########################################
###APPLY OPTIONS, RUN SELECTED FUNCTION###
##########################################
if [[ $1 = "add" ]]; then
addLocation="$2"
while ! [[ $addLocation ]]; do
echo -en "${bold}${yellow}You have chosen to add a torrent file or magnet URL, but have not specified one. Please specify a file location or magnet URL. ${reset}"
read addLocation
done
findJail
addTorrent
elif [[ $1 = "" ]]; then
echo -e "${bold}${yellow}You haven't chosen an option. Exiting.${reset}"
elif [[ $1 = "list" ]]; then
findJail
listTorrents
elif [[ $1 = "stopAll" ]]; then
findJail
stopAllTorrents
elif [[ $1 = "startAll" ]]; then
findJail
startAllTorrents
elif [[ $1 = "stop" ]]; then
torrent="$2"
findJail
listTorrents $torrent
stopTorrent
elif [[ $1 = "start" ]]; then
torrent="$2"
findJail
listTorrents $torrent
startTorrent
elif [[ $1 = "remove" ]]; then
torrent="$2"
findJail
listTorrents $torrent
removeTorrent
elif [[ $1 = "removeAll" ]]; then
findJail
removeAllTorrents
else
echo -e "${bold}${yellow}You have chosen an invalid option "$1". Exiting.${reset}"
fi