tor-ctrl.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/bin/bash
  2. #
  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):
  9. #
  10. # echo "HashedControlPassword $(tor --hash-password yourpassword | tail -n 1)"
  11. #
  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.
  15. #
  16. # For setting the bandwidth for specific times of the day, I suggest calling
  17. # tor-ctrl via cron, e.g.:
  18. #
  19. # 0 22 * * * /path/to/tor-ctrl -c "SETCONF bandwidthrate=1mb"
  20. # 0 7 * * * /path/to/tor-ctrl -c "SETCONF bandwidthrate=100kb"
  21. #
  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.
  24. #
  25. # Many, many other things are possible, see
  26. # https://www.torproject.org/svn/trunk/doc/spec/control-spec.txt
  27. #
  28. # Copyright (c) 2007 by Stefan Behte
  29. #
  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.
  34. #
  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.
  39. #
  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
  43. #
  44. # Written by Stefan Behte
  45. #
  46. # Please send bugs, comments, wishes, thanks and success stories to:
  47. # Stefan dot Behte at gmx dot net
  48. #
  49. # Also have a look at my page:
  50. # http://ge.mine.nu/
  51. #
  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.
  57. VERSION=v1
  58. TORCTLIP=127.0.0.1
  59. TORCTLPORT=9051
  60. TOR_COOKIE="/var/lib/tor/data/control_auth_cookie"
  61. SLEEP_AFTER_CMD=1
  62. VERBOSE=0
  63. usage()
  64. {
  65. cat <<EOF
  66. tor-ctrl $VERSION by Stefan Behte (http://ge.mine.nu)
  67. You should have a look at
  68. https://www.torproject.org/svn/trunk/doc/spec/control-spec.txt
  69. usage: tor-ctrl [-switch] [variable]
  70. [-c] [command] = command to execute
  71. notice: always "quote" your command
  72. [-f] [file] = file to execute commands from
  73. notice: only one command per line
  74. [-a] [path] = path to tor's control_auth_cookie
  75. default: /var/lib/tor/data/control_auth_cookie
  76. notice: do not forget to adjust your torrc
  77. [-s] [time] = sleep [var] seconds after each command sent
  78. default: 1 second
  79. notice: for GETCONF, you can use smaller pause times
  80. than for SETCONF; this is due to telnet's behaviour.
  81. [-p] [pwd] = Use password [var] instead of tor's control_auth_cookie
  82. default: not used
  83. notice: do not forget to adjust your torrc
  84. [-P] [port] = Tor ControlPort
  85. default: 9051
  86. [-v] = verbose
  87. default: not set
  88. notice: the default output is the return code ;)
  89. You propably want to set -v when running manually
  90. Examples: $0 -c "SETCONF bandwidthrate=1mb"
  91. $0 -v -c "GETINFO version"
  92. $0 -v -s 0 -P 9051 -p foobar -c "GETCONF bandwidthrate"
  93. EOF
  94. exit 2
  95. }
  96. checkprogs()
  97. {
  98. programs="telnet"
  99. if [ "$PASSWORD" = "" ]
  100. then
  101. # you only need xxd when using control_auth_cookie
  102. programs="$programs xxd"
  103. fi
  104. for p in $programs
  105. do
  106. which $p &>/dev/null # are you there?
  107. if [ "$?" != "0" ]
  108. then
  109. echo "$p is missing."
  110. exit 2
  111. fi
  112. done
  113. }
  114. sendcmd()
  115. {
  116. echo "$@"
  117. sleep ${SLEEP_AFTER_CMD}
  118. }
  119. login()
  120. {
  121. if [ "$PASSWORD" = "" ]
  122. then
  123. sendcmd "AUTHENTICATE $(xxd -c 32 -g 0 ${TOR_COOKIE} | awk '{print $2}')"
  124. else
  125. sendcmd "AUTHENTICATE \"${PASSWORD}\""
  126. fi
  127. }
  128. cmdpipe()
  129. {
  130. login
  131. sendcmd "$@"
  132. sendcmd "QUIT"
  133. }
  134. vecho()
  135. {
  136. if [ $VERBOSE -ge 1 ]
  137. then
  138. echo "$@"
  139. fi
  140. }
  141. myecho()
  142. {
  143. STR=$(cat)
  144. vecho "$STR"
  145. echo "$STR" | if [ "$(grep -c ^"250 ")" = 3 ]
  146. then
  147. exit 0
  148. else
  149. exit 1
  150. fi
  151. }
  152. filepipe()
  153. {
  154. login
  155. cat "$1" | while read line
  156. do
  157. sendcmd "$line"
  158. done
  159. sendcmd "QUIT"
  160. }
  161. while getopts ":a:c:s:p:P:f:vh" Option
  162. do
  163. case $Option in
  164. a) TOR_COOKIE="${OPTARG}";;
  165. c) CMD="${OPTARG}";;
  166. s) SLEEP_AFTER_CMD="${OPTARG}";;
  167. p) PASSWORD="${OPTARG}";;
  168. P) TORCTLPORT="${OPTARG}";;
  169. f) FILE="${OPTARG}";;
  170. v) VERBOSE=1;;
  171. h) usage;;
  172. *) usage;;
  173. esac
  174. done
  175. if [ -e "$FILE" ]
  176. then
  177. checkprogs
  178. filepipe "$FILE" | telnet $TORCTLIP $TORCTLPORT 2>/dev/null | myecho
  179. exit 4
  180. fi
  181. if [ "$CMD" != "" ]
  182. then
  183. checkprogs
  184. cmdpipe $CMD | telnet $TORCTLIP $TORCTLPORT 2>/dev/null | myecho
  185. else
  186. usage
  187. fi