Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
craig
GitHub Repository: craig/ge.mine.nu
Path: blob/master/lbd/tor-ctrl.sh
100 views
1
#!/bin/bash
2
#
3
# tor-ctrl is a commandline tool for executing commands on a tor server via the controlport.
4
# In order to get this to work, add "ControlPort 9051" and "CookieAuthentication 1" to your torrc and reload tor.
5
# Or - if you want a fixed password - leave out "CookieAuthentication 1" and use the following line to create
6
# the appropriate HashedControlPassword entry for your torrc (you need to change yourpassword, of course):
7
# echo "HashedControlPassword $(tor --hash-password yourpassword | tail -n 1)"
8
#
9
# tor-ctrl will return 0 if it was successful and 1 if not, 2 will be returned if something (telnet, xxd) is missing.
10
# 4 will be returned if it executed serveral commands from a file.
11
#
12
# For setting the bandwidth for specific times of the day, I suggest calling tor-ctrl via cron, e.g.:
13
# 0 22 * * * /path/to/tor-ctrl -c "SETCONF bandwidthrate=1mb"
14
# 0 7 * * * /path/to/tor-ctrl -c "SETCONF bandwidthrate=100kb"
15
#
16
# This would set the bandwidth to 100kb at 07:00 and to 1mb at 22:00.
17
# You can use notations like 1mb, 1kb or the number of bytes.
18
#
19
# Many, many other things are possible, see http://tor.eff.org/svn/trunk/doc/spec/control-spec.txt
20
#
21
# Copyright (c) 2007 by Stefan Behte
22
#
23
# tor-ctrl is free software; you can redistribute it and/or modify
24
# it under the terms of the GNU General Public License as published by
25
# the Free Software Foundation; either version 2 of the License, or
26
# (at your option) any later version.
27
#
28
# tor-ctrl is distributed in the hope that it will be useful,
29
# but WITHOUT ANY WARRANTY; without even the implied warranty of
30
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31
# GNU General Public License for more details.
32
#
33
# You should have received a copy of the GNU General Public License
34
# along with tor-ctrl; if not, write to the Free Software
35
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36
#
37
# Written by Stefan Behte
38
#
39
# Please send bugs, comments, wishes, thanks and success stories to:
40
# Stefan dot Behte at gmx dot net
41
#
42
# Also have a look at my page:
43
# http://ge.mine.nu/
44
#
45
# 2007-10-03: First version, only changing bandwidth possible
46
# 2007-10-04: Renaming to "tor-ctrl", added a lot of functions, it's now a general-purpose tool
47
# added control_auth_cookie/controlpassword auth, getopts, program checks, readinf from file etc.
48
49
VERSION=v1
50
TORCTLIP=127.0.0.1
51
TORCTLPORT=9051
52
TOR_COOKIE="/var/lib/tor/data/control_auth_cookie"
53
SLEEP_AFTER_CMD=1
54
VERBOSE=0
55
56
usage()
57
{
58
cat <<EOF
59
60
tor-ctrl $VERSION by Stefan Behte (http://ge.mine.nu)
61
You should have a look at http://tor.eff.org/svn/trunk/doc/spec/control-spec.txt
62
63
usage: tor-ctrl [-switch] [variable]
64
65
[-c] [command] = command to execute
66
notice: always "quote" your command
67
68
[-f] [file] = file to execute commands from
69
notice: only one command per line
70
71
[-a] [path] = path to tor's control_auth_cookie
72
default: /var/lib/tor/data/control_auth_cookie
73
notice: do not forget to adjust your torrc
74
75
[-s] [time] = sleep [var] seconds after each command sent
76
default: 1 second
77
notice: for GETCONF, you can use smaller pause times than for SETCONF
78
this is due to telnet's behaviour.
79
80
[-p] [pwd] = Use password [var] instead of tor's control_auth_cookie
81
default: not used
82
notice: do not forget to adjust your torrc
83
84
[-P] [port] = Tor ControlPort
85
default: 9051
86
87
[-v] = verbose
88
default: not set
89
notice: the default output is the return code ;)
90
You propably want to set -v when running manually
91
92
Examples: $0 -c "SETCONF bandwidthrate=1mb"
93
$0 -v -c "GETINFO version"
94
$0 -v -s 0 -P 9051 -p foobar -c "GETCONF bandwidthrate"
95
96
EOF
97
exit 2
98
}
99
100
checkprogs()
101
{
102
programs="telnet"
103
if [ "$PASSWORD" = "" ] # you only need xxd when using the control_auth_cookie
104
then
105
programs="$programs xxd"
106
fi
107
108
for p in $programs
109
do
110
which $p &>/dev/null # are you there?
111
if [ "$?" != "0" ]
112
then
113
echo "$p is missing."
114
exit 2
115
fi
116
done
117
}
118
119
sendcmd()
120
{
121
echo "$@"
122
sleep ${SLEEP_AFTER_CMD}
123
}
124
125
login()
126
{
127
if [ "$PASSWORD" = "" ]
128
then
129
sendcmd "AUTHENTICATE $(xxd -c 32 -g 0 ${TOR_COOKIE} | awk '{print $2}')"
130
else
131
sendcmd "AUTHENTICATE \"${PASSWORD}\""
132
fi
133
}
134
135
cmdpipe()
136
{
137
login
138
sendcmd "$@"
139
sendcmd "QUIT"
140
}
141
142
vecho()
143
{
144
if [ $VERBOSE -ge 1 ]
145
then
146
echo "$@"
147
fi
148
}
149
150
myecho()
151
{
152
STR=$(cat)
153
vecho "$STR"
154
155
echo "$STR" | if [ "$(grep -c ^"250 ")" = 3 ]
156
then
157
exit 0
158
else
159
exit 1
160
fi
161
}
162
163
filepipe()
164
{
165
login
166
cat "$1" | while read line
167
do
168
sendcmd "$line"
169
done
170
sendcmd "QUIT"
171
}
172
173
while getopts ":a:c:s:p:P:f:vh" Option
174
do
175
case $Option in
176
a) TOR_COOKIE="${OPTARG}";;
177
c) CMD="${OPTARG}";;
178
s) SLEEP_AFTER_CMD="${OPTARG}";;
179
p) PASSWORD="${OPTARG}";;
180
P) TORCTLPORT="${OPTARG}";;
181
f) FILE="${OPTARG}";;
182
v) VERBOSE=1;;
183
h) usage;;
184
*) usage;;
185
esac
186
done
187
188
if [ -e "$FILE" ]
189
then
190
checkprogs
191
filepipe "$FILE" | telnet $TORCTLIP $TORCTLPORT 2>/dev/null | myecho
192
exit 4
193
fi
194
195
if [ "$CMD" != "" ]
196
then
197
checkprogs
198
cmdpipe $CMD | telnet $TORCTLIP $TORCTLPORT 2>/dev/null | myecho
199
else
200
usage
201
fi
202
203