|
@@ -0,0 +1,201 @@
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+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 http://tor.eff.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
|
|
|
+ programs="$programs xxd"
|
|
|
+ fi
|
|
|
+
|
|
|
+ for p in $programs
|
|
|
+ do
|
|
|
+ which $p &>/dev/null
|
|
|
+ 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
|