3 # tor-ctrl is a commandline tool for executing commands on a tor server via
4 # the controlport. In order to get this to work, add "ControlPort 9051" and
5 # "CookieAuthentication 1" to your torrc and reload tor. Or - if you want a
6 # fixed password - leave out "CookieAuthentication 1" and use the following
7 # line to create the appropriate HashedControlPassword entry for your torrc
8 # (you need to change yourpassword, of course):
10 # echo "HashedControlPassword $(tor --hash-password yourpassword | tail -n 1)"
12 # tor-ctrl will return 0 if it was successful and 1 if not, 2 will be returned
13 # if something (telnet, xxd) is missing. 4 will be returned if it executed
14 # several commands from a file.
16 # For setting the bandwidth for specific times of the day, I suggest calling
17 # tor-ctrl via cron, e.g.:
19 # 0 22 * * * /path/to/tor-ctrl -c "SETCONF bandwidthrate=1mb"
20 # 0 7 * * * /path/to/tor-ctrl -c "SETCONF bandwidthrate=100kb"
22 # This would set the bandwidth to 100kb at 07:00 and to 1mb at 22:00. You can
23 # use notations like 1mb, 1kb or the number of bytes.
25 # Many, many other things are possible, see
26 # https://www.torproject.org/svn/trunk/doc/spec/control-spec.txt
28 # Copyright (c) 2007 by Stefan Behte
30 # tor-ctrl is free software; you can redistribute it and/or modify
31 # it under the terms of the GNU General Public License as published by
32 # the Free Software Foundation; either version 2 of the License, or
33 # (at your option) any later version.
35 # tor-ctrl is distributed in the hope that it will be useful,
36 # but WITHOUT ANY WARRANTY; without even the implied warranty of
37 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 # GNU General Public License for more details.
40 # You should have received a copy of the GNU General Public License
41 # along with tor-ctrl; if not, write to the Free Software
42 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
44 # Written by Stefan Behte
46 # Please send bugs, comments, wishes, thanks and success stories to:
47 # Stefan dot Behte at gmx dot net
49 # Also have a look at my page:
52 # 2007-10-03: First version, only changing bandwidth possible.
53 # 2007-10-04: Renaming to "tor-ctrl", added a lot of functions, it's now a
54 # general-purpose tool.
55 # Added control_auth_cookie/controlpassword auth, getopts,
56 # program checks, reading from file etc.
61 TOR_COOKIE
="/var/lib/tor/data/control_auth_cookie"
69 tor-ctrl $VERSION by Stefan Behte (http://ge.mine.nu)
70 You should have a look at
71 https://www.torproject.org/svn/trunk/doc/spec/control-spec.txt
73 usage: tor-ctrl [-switch] [variable]
75 [-c] [command] = command to execute
76 notice: always "quote" your command
78 [-f] [file] = file to execute commands from
79 notice: only one command per line
81 [-a] [path] = path to tor's control_auth_cookie
82 default: /var/lib/tor/data/control_auth_cookie
83 notice: do not forget to adjust your torrc
85 [-s] [time] = sleep [var] seconds after each command sent
87 notice: for GETCONF, you can use smaller pause times
88 than for SETCONF; this is due to telnet's behaviour.
90 [-p] [pwd] = Use password [var] instead of tor's control_auth_cookie
92 notice: do not forget to adjust your torrc
94 [-P] [port] = Tor ControlPort
99 notice: the default output is the return code ;)
100 You propably want to set -v when running manually
102 Examples: $0 -c "SETCONF bandwidthrate=1mb"
103 $0 -v -c "GETINFO version"
104 $0 -v -s 0 -P 9051 -p foobar -c "GETCONF bandwidthrate"
113 if [ "$PASSWORD" = "" ]
115 # you only need xxd when using control_auth_cookie
116 programs
="$programs xxd"
121 which $p &>/dev
/null
# are you there?
124 echo "$p is missing."
133 sleep ${SLEEP_AFTER_CMD}
138 if [ "$PASSWORD" = "" ]
140 sendcmd
"AUTHENTICATE $(xxd -c 32 -g 0 ${TOR_COOKIE} | awk '{print $2}')"
142 sendcmd
"AUTHENTICATE \"${PASSWORD}\""
155 if [ $VERBOSE -ge 1 ]
166 echo "$STR" |
if [ "$(grep -c ^"250 ")" = 3 ]
177 cat "$1" |
while read line
184 while getopts ":a:c:s:p:P:f:vh" Option
187 a
) TOR_COOKIE
="${OPTARG}";;
189 s
) SLEEP_AFTER_CMD
="${OPTARG}";;
190 p
) PASSWORD
="${OPTARG}";;
191 P
) TORCTLPORT
="${OPTARG}";;
192 f
) FILE
="${OPTARG}";;
202 filepipe
"$FILE" |
telnet $TORCTLIP $TORCTLPORT 2>/dev
/null | myecho
209 cmdpipe
$CMD |
telnet $TORCTLIP $TORCTLPORT 2>/dev
/null | myecho