configuration-templates – Rev 168

Subversion Repositories:
Rev:
#!/bin/bash
###########################################################################
##  Copyright (C) Wizardry and Steamworks 2023 - License: GNU GPLv3      ##
###########################################################################
# This script connects to rTorrent via SCGI, checks the hostname and the  #
# current PID of the rTorrent process and then returns 0 if the hostname  #
# and PID match the results from the system tools or 1 otherwise.         #
#                                                                         #
# Requirements:                                                           #
#   * sed                                                                 #
#   * tr                                                                  #
#   * netcat                                                              #
#   * grep                                                                #
#   * xmlstarlet                                                          #
#                                                                         #
###########################################################################

###########################################################################
#                           CONFIGURATION                                 #
###########################################################################

# These settings correspond to the rTorrent setting:
#     network.scgi.open_port = "0.0.0.0:5000"
# that enables the SCGI remote access functionality.
RTORRENT_HOST="localhost"
RTORRENT_PORT=5000

###########################################################################
#                             INTERNALS                                   #
###########################################################################

SESSION=$(cat <<EOF | \
    sed -E 's/[ ]{2,}//g' | \
    tr ' ' \\0 | \
    tr -d '\n' | \
    netcat "$RTORRENT_HOST" "$RTORRENT_PORT" | \
    grep -Pzo "(?s)<methodResponse>.+?<\/methodResponse>" | \
    xmlstarlet select -t -v /methodResponse/params/param[1]/value/string
63:
    CONTENT_LENGTH 62 
    SCGI 1 
    REQUEST_METHOD POST 
    REQUEST_URI /RPC2 
,
<methodCall>
    <methodName>session.name</methodName>
</methodCall>
EOF
)

HOST=`echo $SESSION | awk -F':' '{ print $1 }'`
PID=`echo $SESSION | awk -F':' '{ print $2 }'`

[ "$HOST" == `hostname` ] || exit 1
[ "$PID" == `pgrep -f /usr/bin/rtorrent` ] || exit 1

exit 0