Ensure node is a guard candidate when picking a directory guard
[tor.git] / contrib / tor-ctrl.sh
blob58320ced12172831979ebfe44d75773d436158be
1 #!/bin/bash
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:
50 # http://ge.mine.nu/
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.
58 VERSION=v1
59 TORCTLIP=127.0.0.1
60 TORCTLPORT=9051
61 TOR_COOKIE="/var/lib/tor/data/control_auth_cookie"
62 SLEEP_AFTER_CMD=1
63 VERBOSE=0
65 usage()
67 cat <<EOF
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
86 default: 1 second
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
91 default: not used
92 notice: do not forget to adjust your torrc
94 [-P] [port] = Tor ControlPort
95 default: 9051
97 [-v] = verbose
98 default: not set
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"
107 exit 2
110 checkprogs()
112 programs="telnet"
113 if [ "$PASSWORD" = "" ]
114 then
115 # you only need xxd when using control_auth_cookie
116 programs="$programs xxd"
119 for p in $programs
121 which $p &>/dev/null # are you there?
122 if [ "$?" != "0" ]
123 then
124 echo "$p is missing."
125 exit 2
127 done
130 sendcmd()
132 echo "$@"
133 sleep ${SLEEP_AFTER_CMD}
136 login()
138 if [ "$PASSWORD" = "" ]
139 then
140 sendcmd "AUTHENTICATE $(xxd -c 32 -g 0 ${TOR_COOKIE} | awk '{print $2}')"
141 else
142 sendcmd "AUTHENTICATE \"${PASSWORD}\""
146 cmdpipe()
148 login
149 sendcmd "$@"
150 sendcmd "QUIT"
153 vecho()
155 if [ $VERBOSE -ge 1 ]
156 then
157 echo "$@"
161 myecho()
163 STR=$(cat)
164 vecho "$STR"
166 echo "$STR" | if [ "$(grep -c ^"250 ")" = 3 ]
167 then
168 exit 0
169 else
170 exit 1
174 filepipe()
176 login
177 cat "$1" | while read line
179 sendcmd "$line"
180 done
181 sendcmd "QUIT"
184 while getopts ":a:c:s:p:P:f:vh" Option
186 case $Option in
187 a) TOR_COOKIE="${OPTARG}";;
188 c) CMD="${OPTARG}";;
189 s) SLEEP_AFTER_CMD="${OPTARG}";;
190 p) PASSWORD="${OPTARG}";;
191 P) TORCTLPORT="${OPTARG}";;
192 f) FILE="${OPTARG}";;
193 v) VERBOSE=1;;
194 h) usage;;
195 *) usage;;
196 esac
197 done
199 if [ -e "$FILE" ]
200 then
201 checkprogs
202 filepipe "$FILE" | telnet $TORCTLIP $TORCTLPORT 2>/dev/null | myecho
203 exit 4
206 if [ "$CMD" != "" ]
207 then
208 checkprogs
209 cmdpipe $CMD | telnet $TORCTLIP $TORCTLPORT 2>/dev/null | myecho
210 else
211 usage