tor-ctrl.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. # serveral 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. # http://tor.eff.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 added control_auth_cookie/controlpassword
  55. # auth, getopts, program checks, readinf from file etc.
  56. VERSION=v1
  57. TORCTLIP=127.0.0.1
  58. TORCTLPORT=9051
  59. TOR_COOKIE="/var/lib/tor/data/control_auth_cookie"
  60. SLEEP_AFTER_CMD=1
  61. VERBOSE=0
  62. usage()
  63. {
  64. cat <<EOF
  65. tor-ctrl $VERSION by Stefan Behte (http://ge.mine.nu)
  66. You should have a look at
  67. http://tor.eff.org/svn/trunk/doc/spec/control-spec.txt
  68. usage: tor-ctrl [-switch] [variable]
  69. [-c] [command] = command to execute
  70. notice: always "quote" your command
  71. [-f] [file] = file to execute commands from
  72. notice: only one command per line
  73. [-a] [path] = path to tor's control_auth_cookie
  74. default: /var/lib/tor/data/control_auth_cookie
  75. notice: do not forget to adjust your torrc
  76. [-s] [time] = sleep [var] seconds after each command sent
  77. default: 1 second
  78. notice: for GETCONF, you can use smaller pause times
  79. than for SETCONF; this is due to telnet's behaviour.
  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. [-P] [port] = Tor ControlPort
  84. default: 9051
  85. [-v] = verbose
  86. default: not set
  87. notice: the default output is the return code ;)
  88. You propably want to set -v when running manually
  89. Examples: $0 -c "SETCONF bandwidthrate=1mb"
  90. $0 -v -c "GETINFO version"
  91. $0 -v -s 0 -P 9051 -p foobar -c "GETCONF bandwidthrate"
  92. EOF
  93. exit 2
  94. }
  95. checkprogs()
  96. {
  97. programs="telnet"
  98. if [ "$PASSWORD" = "" ]
  99. then
  100. # you only need xxd when using control_auth_cookie
  101. programs="$programs xxd"
  102. fi
  103. for p in $programs
  104. do
  105. which $p &>/dev/null # are you there?
  106. if [ "$?" != "0" ]
  107. then
  108. echo "$p is missing."
  109. exit 2
  110. fi
  111. done
  112. }
  113. sendcmd()
  114. {
  115. echo "$@"
  116. sleep ${SLEEP_AFTER_CMD}
  117. }
  118. login()
  119. {
  120. if [ "$PASSWORD" = "" ]
  121. then
  122. sendcmd "AUTHENTICATE $(xxd -c 32 -g 0 ${TOR_COOKIE} | awk '{print $2}')"
  123. else
  124. sendcmd "AUTHENTICATE \"${PASSWORD}\""
  125. fi
  126. }
  127. cmdpipe()
  128. {
  129. login
  130. sendcmd "$@"
  131. sendcmd "QUIT"
  132. }
  133. vecho()
  134. {
  135. if [ $VERBOSE -ge 1 ]
  136. then
  137. echo "$@"
  138. fi
  139. }
  140. myecho()
  141. {
  142. STR=$(cat)
  143. vecho "$STR"
  144. echo "$STR" | if [ "$(grep -c ^"250 ")" = 3 ]
  145. then
  146. exit 0
  147. else
  148. exit 1
  149. fi
  150. }
  151. filepipe()
  152. {
  153. login
  154. cat "$1" | while read line
  155. do
  156. sendcmd "$line"
  157. done
  158. sendcmd "QUIT"
  159. }
  160. while getopts ":a:c:s:p:P:f:vh" Option
  161. do
  162. case $Option in
  163. a) TOR_COOKIE="${OPTARG}";;
  164. c) CMD="${OPTARG}";;
  165. s) SLEEP_AFTER_CMD="${OPTARG}";;
  166. p) PASSWORD="${OPTARG}";;
  167. P) TORCTLPORT="${OPTARG}";;
  168. f) FILE="${OPTARG}";;
  169. v) VERBOSE=1;;
  170. h) usage;;
  171. *) usage;;
  172. esac
  173. done
  174. if [ -e "$FILE" ]
  175. then
  176. checkprogs
  177. filepipe "$FILE" | telnet $TORCTLIP $TORCTLPORT 2>/dev/null | myecho
  178. exit 4
  179. fi
  180. if [ "$CMD" != "" ]
  181. then
  182. checkprogs
  183. cmdpipe $CMD | telnet $TORCTLIP $TORCTLPORT 2>/dev/null | myecho
  184. else
  185. usage
  186. fi