tor-ctrl.sh 5.3 KB

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