#!/bin/bash1#2# tor-ctrl is a commandline tool for executing commands on a tor server via the controlport.3# In order to get this to work, add "ControlPort 9051" and "CookieAuthentication 1" to your torrc and reload tor.4# Or - if you want a fixed password - leave out "CookieAuthentication 1" and use the following line to create5# the appropriate HashedControlPassword entry for your torrc (you need to change yourpassword, of course):6# echo "HashedControlPassword $(tor --hash-password yourpassword | tail -n 1)"7#8# tor-ctrl will return 0 if it was successful and 1 if not, 2 will be returned if something (telnet, xxd) is missing.9# 4 will be returned if it executed serveral commands from a file.10#11# For setting the bandwidth for specific times of the day, I suggest calling tor-ctrl via cron, e.g.:12# 0 22 * * * /path/to/tor-ctrl -c "SETCONF bandwidthrate=1mb"13# 0 7 * * * /path/to/tor-ctrl -c "SETCONF bandwidthrate=100kb"14#15# This would set the bandwidth to 100kb at 07:00 and to 1mb at 22:00.16# You can use notations like 1mb, 1kb or the number of bytes.17#18# Many, many other things are possible, see http://tor.eff.org/svn/trunk/doc/spec/control-spec.txt19#20# Copyright (c) 2007 by Stefan Behte21#22# tor-ctrl is free software; you can redistribute it and/or modify23# it under the terms of the GNU General Public License as published by24# the Free Software Foundation; either version 2 of the License, or25# (at your option) any later version.26#27# tor-ctrl is distributed in the hope that it will be useful,28# but WITHOUT ANY WARRANTY; without even the implied warranty of29# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the30# GNU General Public License for more details.31#32# You should have received a copy of the GNU General Public License33# along with tor-ctrl; if not, write to the Free Software34# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA35#36# Written by Stefan Behte37#38# Please send bugs, comments, wishes, thanks and success stories to:39# Stefan dot Behte at gmx dot net40#41# Also have a look at my page:42# http://ge.mine.nu/43#44# 2007-10-03: First version, only changing bandwidth possible45# 2007-10-04: Renaming to "tor-ctrl", added a lot of functions, it's now a general-purpose tool46# added control_auth_cookie/controlpassword auth, getopts, program checks, readinf from file etc.4748VERSION=v149TORCTLIP=127.0.0.150TORCTLPORT=905151TOR_COOKIE="/var/lib/tor/data/control_auth_cookie"52SLEEP_AFTER_CMD=153VERBOSE=05455usage()56{57cat <<EOF5859tor-ctrl $VERSION by Stefan Behte (http://ge.mine.nu)60You should have a look at http://tor.eff.org/svn/trunk/doc/spec/control-spec.txt6162usage: tor-ctrl [-switch] [variable]6364[-c] [command] = command to execute65notice: always "quote" your command6667[-f] [file] = file to execute commands from68notice: only one command per line6970[-a] [path] = path to tor's control_auth_cookie71default: /var/lib/tor/data/control_auth_cookie72notice: do not forget to adjust your torrc7374[-s] [time] = sleep [var] seconds after each command sent75default: 1 second76notice: for GETCONF, you can use smaller pause times than for SETCONF77this is due to telnet's behaviour.7879[-p] [pwd] = Use password [var] instead of tor's control_auth_cookie80default: not used81notice: do not forget to adjust your torrc8283[-P] [port] = Tor ControlPort84default: 90518586[-v] = verbose87default: not set88notice: the default output is the return code ;)89You propably want to set -v when running manually9091Examples: $0 -c "SETCONF bandwidthrate=1mb"92$0 -v -c "GETINFO version"93$0 -v -s 0 -P 9051 -p foobar -c "GETCONF bandwidthrate"9495EOF96exit 297}9899checkprogs()100{101programs="telnet"102if [ "$PASSWORD" = "" ] # you only need xxd when using the control_auth_cookie103then104programs="$programs xxd"105fi106107for p in $programs108do109which $p &>/dev/null # are you there?110if [ "$?" != "0" ]111then112echo "$p is missing."113exit 2114fi115done116}117118sendcmd()119{120echo "$@"121sleep ${SLEEP_AFTER_CMD}122}123124login()125{126if [ "$PASSWORD" = "" ]127then128sendcmd "AUTHENTICATE $(xxd -c 32 -g 0 ${TOR_COOKIE} | awk '{print $2}')"129else130sendcmd "AUTHENTICATE \"${PASSWORD}\""131fi132}133134cmdpipe()135{136login137sendcmd "$@"138sendcmd "QUIT"139}140141vecho()142{143if [ $VERBOSE -ge 1 ]144then145echo "$@"146fi147}148149myecho()150{151STR=$(cat)152vecho "$STR"153154echo "$STR" | if [ "$(grep -c ^"250 ")" = 3 ]155then156exit 0157else158exit 1159fi160}161162filepipe()163{164login165cat "$1" | while read line166do167sendcmd "$line"168done169sendcmd "QUIT"170}171172while getopts ":a:c:s:p:P:f:vh" Option173do174case $Option in175a) TOR_COOKIE="${OPTARG}";;176c) CMD="${OPTARG}";;177s) SLEEP_AFTER_CMD="${OPTARG}";;178p) PASSWORD="${OPTARG}";;179P) TORCTLPORT="${OPTARG}";;180f) FILE="${OPTARG}";;181v) VERBOSE=1;;182h) usage;;183*) usage;;184esac185done186187if [ -e "$FILE" ]188then189checkprogs190filepipe "$FILE" | telnet $TORCTLIP $TORCTLPORT 2>/dev/null | myecho191exit 4192fi193194if [ "$CMD" != "" ]195then196checkprogs197cmdpipe $CMD | telnet $TORCTLIP $TORCTLPORT 2>/dev/null | myecho198else199usage200fi201202203