Commit fce82318 authored by Nick Mathewson's avatar Nick Mathewson
Browse files

r15925@catbus: nickm | 2007-10-18 14:13:57 -0400

 Add tor-ctrl.sh to dist, and make it fit in 80-col terminals.


svn:r12029
parent 4807d8c9
......@@ -3,7 +3,7 @@ DIST_SUBDIRS = osx suse
confdir = $(sysconfdir)/tor
EXTRA_DIST = exitlist tor-tsocks.conf torify.1 tor.nsi.in tor.sh torctl rc.subr cross.sh tor-mingw.nsi.in package_nsis-mingw.sh tor.ico
EXTRA_DIST = exitlist tor-tsocks.conf torify.1 tor.nsi.in tor.sh torctl rc.subr cross.sh tor-mingw.nsi.in package_nsis-mingw.sh tor.ico tor-ctrl.sh
conf_DATA = tor-tsocks.conf
......
#!/bin/bash
#
# tor-ctrl is a commandline tool for executing commands on a tor server via the controlport.
# In order to get this to work, add "ControlPort 9051" and "CookieAuthentication 1" to your torrc and reload tor.
# Or - if you want a fixed password - leave out "CookieAuthentication 1" and use the following line to create
# the appropriate HashedControlPassword entry for your torrc (you need to change yourpassword, of course):
# tor-ctrl is a commandline tool for executing commands on a tor server via
# the controlport. In order to get this to work, add "ControlPort 9051" and
# "CookieAuthentication 1" to your torrc and reload tor. Or - if you want a
# fixed password - leave out "CookieAuthentication 1" and use the following
# line to create the appropriate HashedControlPassword entry for your torrc
# (you need to change yourpassword, of course):
#
# echo "HashedControlPassword $(tor --hash-password yourpassword | tail -n 1)"
#
# tor-ctrl will return 0 if it was successful and 1 if not, 2 will be returned if something (telnet, xxd) is missing.
# 4 will be returned if it executed serveral commands from a file.
# tor-ctrl will return 0 if it was successful and 1 if not, 2 will be returned
# if something (telnet, xxd) is missing. 4 will be returned if it executed
# serveral commands from a file.
#
# For setting the bandwidth for specific times of the day, I suggest calling
# tor-ctrl via cron, e.g.:
#
# For setting the bandwidth for specific times of the day, I suggest calling tor-ctrl via cron, e.g.:
# 0 22 * * * /path/to/tor-ctrl -c "SETCONF bandwidthrate=1mb"
# 0 7 * * * /path/to/tor-ctrl -c "SETCONF bandwidthrate=100kb"
#
# This would set the bandwidth to 100kb at 07:00 and to 1mb at 22:00.
# You can use notations like 1mb, 1kb or the number of bytes.
# This would set the bandwidth to 100kb at 07:00 and to 1mb at 22:00. You can
# use notations like 1mb, 1kb or the number of bytes.
#
# Many, many other things are possible, see http://tor.eff.org/svn/trunk/doc/spec/control-spec.txt
# Many, many other things are possible, see
# http://tor.eff.org/svn/trunk/doc/spec/control-spec.txt
#
# Copyright (c) 2007 by Stefan Behte
#
......@@ -43,8 +50,9 @@
# http://ge.mine.nu/
#
# 2007-10-03: First version, only changing bandwidth possible
# 2007-10-04: Renaming to "tor-ctrl", added a lot of functions, it's now a general-purpose tool
# added control_auth_cookie/controlpassword auth, getopts, program checks, readinf from file etc.
# 2007-10-04: Renaming to "tor-ctrl", added a lot of functions, it's now a
# general-purpose tool added control_auth_cookie/controlpassword
# auth, getopts, program checks, readinf from file etc.
VERSION=v1
TORCTLIP=127.0.0.1
......@@ -58,40 +66,41 @@ 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
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
[-c] [command] = command to execute
notice: always "quote" your command
[-f] [file] = file to execute commands from
notice: only one command per line
[-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
[-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.
[-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
[-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
[-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"
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
......@@ -99,103 +108,104 @@ exit 2
checkprogs()
{
programs="telnet"
if [ "$PASSWORD" = "" ] # you only need xxd when using the control_auth_cookie
then
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
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}
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
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"
login
sendcmd "$@"
sendcmd "QUIT"
}
vecho()
{
if [ $VERBOSE -ge 1 ]
then
echo "$@"
fi
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
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"
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
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
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
checkprogs
cmdpipe $CMD | telnet $TORCTLIP $TORCTLPORT 2>/dev/null | myecho
else
usage
usage
fi
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment