Skip to content

Commit

Permalink
make *-c* an optional option if command is the last positional argument
Browse files Browse the repository at this point in the history
if the command was in the middle and not preceded by the -c, getopts would exit there and not parse options after it, which is not what we want. By making it the last positional argument, we avoid that error.
  • Loading branch information
nyxnor committed Jan 10, 2022
1 parent df0e0db commit 73c5ae0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -76,14 +76,14 @@ See usage:
tor-ctrl -h
```

Get your tor user:
Get your tor user (the **-c** is optional when the command is the last positional argument):
```sh
tor-ctrl -c "GETCONF User"
```

Get your circuits (raw):
```sh
tor-ctrl -c "GETINFO circuit-status"
tor-ctrl "GETINFO circuit-status"
```

That is not very clean to read, too much information, so lets organize it.
Expand Down
21 changes: 14 additions & 7 deletions bin/tor-ctrl
Expand Up @@ -60,6 +60,7 @@ usage(){
-c [command] command to execute
notice: always \"double quote\" your command
notice: separate control commands with a pipe '|'
notice: '-c' is optional when command is the last positional argument
-s [socket] tor's control socket
default: 9051
Expand All @@ -79,11 +80,11 @@ usage(){
-q quiet mode. (Default: not set).
Examples: ${me} -q -c \"SETCONF bandwidthrate=1mb\"
${me} -s 9051 -p foobar -c \"GETCONF bandwidthrate\"
${me} -c \"GETINFO config-defaults-file config-file config-text\"
${me} -c \"GETCONF SocksPort | GETINFO net/listeners/socks\"
${me} -w -c \"SETEVENTS STREAM\"
Examples: ${me} -q \"SETCONF bandwidthrate=1mb\"
${me} -s 9051 -p foobar \"GETCONF bandwidthrate\"
${me} \"GETINFO config-defaults-file config-file config-text\"
${me} \"GETCONF SocksPort | GETINFO net/listeners/socks\"
${me} -c \"SETEVENTS STREAM\" -w
You should have a look at
https://gitweb.torproject.org/torspec.git/tree/control-spec.txt"
Expand Down Expand Up @@ -201,10 +202,16 @@ while getopts ":c:s:p:t:qwh" Option; do
t) sleep_n="${OPTARG}";;
q) quiet=1;;
w) wait_confirmation=1;;
h|*) usage;;
h) usage;;
*) usage;;
esac
done

if [ -z "${cmd_cli}" ]; then
[ "${OPTIND}" != "${#}" ] && error_msg "Command must be the last positional argument or preceded with the '-c' option"
eval cmd_cli='$'"${OPTIND}"
fi

## accept commands separated by pipes
cmd_cli="$(printf '%s\n' "${cmd_cli}" | tr -s "|" "\n" | sed "s|^ ||")"

Expand All @@ -227,7 +234,7 @@ case "${tor_control_socket}" in
[ "${tor_control_host}" = "${tor_control_port}" ] && tor_control_host="127.0.0.1"
for quad in $(printf '%s\n' "${tor_control_host}" | tr "." " "); do
printf %d "${quad}" >/dev/null 2>&1 || error_msg "${tor_control_host} is not a valid address, ${quad} is not and integer"
{ [ "${quad}" -ge 0 ] && [ "${quad}" -le 255 ]; } || error_msg "${tor_control_host}is not a valid address, ${quad} is not within range: 0-255"
{ [ "${quad}" -ge 0 ] && [ "${quad}" -le 255 ]; } || error_msg "${tor_control_host} is not a valid address, ${quad} is not within range: 0-255"
done
;;
esac
Expand Down
4 changes: 3 additions & 1 deletion configure.sh
Expand Up @@ -6,7 +6,9 @@ case "${1}" in
install)
[ "$(id -u)" -ne 0 ] && printf '%s\n' "Run ${me} as root." && exit 1
cp bin/tor-ctrl /usr/local/bin/
cp man/tor-ctrl.8 /usr/local/man/man8/
for mandir in "/usr/local/man/man8" "/usr/local/share/man/man8" "/usr/share/man/man8"; do
[ -d "${mandir}" ] && cp man/tor-ctrl.8 "${mandir}" && break
done
;;
man)
command -v pandoc >/dev/null || printf '%s\n' "Install 'pandoc' to create manual pages"
Expand Down
11 changes: 5 additions & 6 deletions docs/tor-ctrl.8.md
Expand Up @@ -8,7 +8,7 @@ tor-ctrl(8) - Interact with Tor's controller via command line tool

# SYNOPSIS

**tor-ctrl** [-switch] [variable]
**tor-ctrl** [**-wq**] [**-s** *socket*] [**-p** *pwd*] [**-t** *time*] [[**-c**] *command*]

# DESCRIPTION

Expand All @@ -18,8 +18,7 @@ tor-ctrl(8) - Interact with Tor's controller via command line tool
# OPTIONS

**-c** [*command*]
: command to execute
notice: always "quote" your command
: command to execute. Always "quote" your command. The *-c* is optional if the command is the last positional argument.

**-s** [*socket*]
: Tor's control socket. Accept *tcp socket* in the format [*addr:*]*port* (examples: 9051, 127.0.0.1:9051). Accept *unix domain socket* in the following format [*unix:*]*path* (examples: /run/tor/control, unix:/run/tor/control). (Default: 9051).
Expand All @@ -46,11 +45,11 @@ notice: always "quote" your command

# EXAMPLES

tor-ctrl -c "SETCONF bandwidthrate=1mb"
tor-ctrl -c "SETCONF bandwidthrate=1mb" -q

tor-ctrl -c "GETINFO version"
tor-ctrl "GETINFO version"

tor-ctrl -s 9051 -p foobar -c "GETCONF bandwidthrate"
tor-ctrl -s 9051 -p foobar "GETCONF bandwidthrate"

For setting the bandwidth for specific times of the day, I suggest calling tor-ctrl via cron, e.g.:

Expand Down
15 changes: 10 additions & 5 deletions man/tor-ctrl.8
Expand Up @@ -7,7 +7,9 @@
tor-ctrl(8) - Interact with Tor\[aq]s controller via command line tool
.SH SYNOPSIS
.PP
\f[B]tor-ctrl\f[R] [-switch] [variable]
\f[B]tor-ctrl\f[R] [\f[B]-wq\f[R]] [\f[B]-s\f[R] \f[I]socket\f[R]]
[\f[B]-p\f[R] \f[I]pwd\f[R]] [\f[B]-t\f[R] \f[I]time\f[R]]
[[\f[B]-c\f[R]] \f[I]command\f[R]]
.SH DESCRIPTION
.PP
\f[B]tor-ctrl\f[R] is a commandline tool for executing commands on a tor
Expand All @@ -25,7 +27,10 @@ reloaded after making changes.
.SH OPTIONS
.TP
\f[B]-c\f[R] [\f[I]command\f[R]]
command to execute notice: always \[dq]quote\[dq] your command
command to execute.
Always \[dq]quote\[dq] your command.
The \f[I]-c\f[R] is optional if the command is the last positional
argument.
.TP
\f[B]-s\f[R] [\f[I]socket\f[R]]
Tor\[aq]s control socket.
Expand Down Expand Up @@ -62,11 +67,11 @@ Fail
Success.
.SH EXAMPLES
.PP
tor-ctrl -c \[dq]SETCONF bandwidthrate=1mb\[dq]
tor-ctrl -c \[dq]SETCONF bandwidthrate=1mb\[dq] -q
.PP
tor-ctrl -c \[dq]GETINFO version\[dq]
tor-ctrl \[dq]GETINFO version\[dq]
.PP
tor-ctrl -s 9051 -p foobar -c \[dq]GETCONF bandwidthrate\[dq]
tor-ctrl -s 9051 -p foobar \[dq]GETCONF bandwidthrate\[dq]
.PP
For setting the bandwidth for specific times of the day, I suggest
calling tor-ctrl via cron, e.g.:
Expand Down

0 comments on commit 73c5ae0

Please sign in to comment.