123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- #!/bin/bash
- VERSION=v1
- TORCTLIP=127.0.0.1
- TORCTLPORT=9051
- TOR_COOKIE="/var/lib/tor/data/control_auth_cookie"
- SLEEP_AFTER_CMD=1
- VERBOSE=0
- usage()
- {
- cat <<EOF
- tor-ctrl $VERSION by Stefan Behte (http://ge.mine.nu)
- You should have a look at
- https://www.torproject.org/svn/trunk/doc/spec/control-spec.txt
- usage: tor-ctrl [-switch] [variable]
- [-c] [command] = command to execute
- notice: always "quote" your command
- [-f] [file] = file to execute commands from
- notice: only one command per line
- [-a] [path] = path to tor's control_auth_cookie
- default: /var/lib/tor/data/control_auth_cookie
- notice: do not forget to adjust your torrc
- [-s] [time] = sleep [var] seconds after each command sent
- default: 1 second
- notice: for GETCONF, you can use smaller pause times
- than for SETCONF; this is due to telnet's behaviour.
- [-p] [pwd] = Use password [var] instead of tor's control_auth_cookie
- default: not used
- notice: do not forget to adjust your torrc
-
- [-P] [port] = Tor ControlPort
- default: 9051
- [-v] = verbose
- default: not set
- notice: the default output is the return code ;)
- You propably want to set -v when running manually
- Examples: $0 -c "SETCONF bandwidthrate=1mb"
- $0 -v -c "GETINFO version"
- $0 -v -s 0 -P 9051 -p foobar -c "GETCONF bandwidthrate"
- EOF
- exit 2
- }
- checkprogs()
- {
- programs="telnet"
- if [ "$PASSWORD" = "" ]
- then
- # you only need xxd when using control_auth_cookie
- programs="$programs xxd"
- fi
- for p in $programs
- do
- which $p &>/dev/null # are you there?
- if [ "$?" != "0" ]
- then
- echo "$p is missing."
- exit 2
- fi
- done
- }
- sendcmd()
- {
- echo "$@"
- sleep ${SLEEP_AFTER_CMD}
- }
- login()
- {
- if [ "$PASSWORD" = "" ]
- then
- sendcmd "AUTHENTICATE $(xxd -c 32 -g 0 ${TOR_COOKIE} | awk '{print $2}')"
- else
- sendcmd "AUTHENTICATE \"${PASSWORD}\""
- fi
- }
- cmdpipe()
- {
- login
- sendcmd "$@"
- sendcmd "QUIT"
- }
- vecho()
- {
- if [ $VERBOSE -ge 1 ]
- then
- echo "$@"
- fi
- }
- myecho()
- {
- STR=$(cat)
- vecho "$STR"
- echo "$STR" | if [ "$(grep -c ^"250 ")" = 3 ]
- then
- exit 0
- else
- exit 1
- fi
- }
- filepipe()
- {
- login
- cat "$1" | while read line
- do
- sendcmd "$line"
- done
- sendcmd "QUIT"
- }
- while getopts ":a:c:s:p:P:f:vh" Option
- do
- case $Option in
- a) TOR_COOKIE="${OPTARG}";;
- c) CMD="${OPTARG}";;
- s) SLEEP_AFTER_CMD="${OPTARG}";;
- p) PASSWORD="${OPTARG}";;
- P) TORCTLPORT="${OPTARG}";;
- f) FILE="${OPTARG}";;
- v) VERBOSE=1;;
- h) usage;;
- *) usage;;
- esac
- done
- if [ -e "$FILE" ]
- then
- checkprogs
- filepipe "$FILE" | telnet $TORCTLIP $TORCTLPORT 2>/dev/null | myecho
- exit 4
- fi
- if [ "$CMD" != "" ]
- then
- checkprogs
- cmdpipe $CMD | telnet $TORCTLIP $TORCTLPORT 2>/dev/null | myecho
- else
- usage
- fi
|